gpt4 book ai didi

ios - 放置在 UIVisualEffectView 顶部的按钮没有响应

转载 作者:行者123 更新时间:2023-11-28 21:46:47 25 4
gpt4 key购买 nike

我正在尝试向 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)
}
}

关于ios - 放置在 UIVisualEffectView 顶部的按钮没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com