UIVisualEffectView에 단추를 추가하려고합니다. 그러나 내가 추가 한 버튼이 터치 이벤트에 응답하지 않으며 이유를 이해할 수 없습니다. 이것이 코드입니다.
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)
}
대상 액션 ( button.addTarget
) 을 추가해야합니다 . 즉, 버튼을 눌렀을 때 수행해야하는 작업은 아래에서 수정 된 코드를 참조하십시오.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a 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 any resources that can be 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)
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다