ios - button placed on top of UIVisualEffectView not responding -
i'm trying add button uivisualeffectview. however, button add not responding touch events , don't understand why. code.
override func viewdidload() { super.viewdidload() let image = uiimage(named: "face2") let imageview = uiimageview(image: image) imageview.frame = cgrectmake(0, 0, 200, 200) let button = uibutton(frame: cgrectmake(50, 50, 100, 100)) button.settitle("huhuhu", forstate: uicontrolstate.normal) button.settitlecolor(uicolor.blackcolor(), forstate: .normal) button.userinteractionenabled = true let effect = uiblureffect(style: .light) let blurview = uivisualeffectview(effect: effect) blurview.frame = view.bounds view.addsubview(imageview) view.addsubview(blurview) view.addsubview(button) }
you need add target action (button.addtarget
), i.e. button supposed when pressed, see code modified below:
class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. let image = uiimage(named: "face2") let imageview = uiimageview(image: image) imageview.frame = cgrectmake(0, 0, 200, 200) let button = uibutton(frame: cgrectmake(50, 50, 100, 100)) button.settitle("huhuhu", forstate: uicontrolstate.normal) button.addtarget(self, action: "sayhi:", forcontrolevents: .touchupinside) button.settitlecolor(uicolor.blackcolor(), forstate: .normal) button.userinteractionenabled = true let effect = uiblureffect(style: .light) let blurview = uivisualeffectview(effect: effect) blurview.frame = view.bounds view.addsubview(imageview) view.addsubview(blurview) view.addsubview(button) } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func sayhi(sender:uibutton) { let randominth = arc4random_uniform(uint32(view.bounds.size.width)-40) let randomintv = arc4random_uniform(uint32(view.bounds.size.height)-15) let frame = cgrectmake(cgfloat(randominth), cgfloat(randomintv), 10, 10) let label = uilabel(frame: frame) label.text = "hallo" label.sizetofit() view.addsubview(label) } }
Comments
Post a Comment