【问题标题】:button placed on top of UIVisualEffectView not responding放置在 UIVisualEffectView 顶部的按钮没有响应
【发布时间】:2015-04-25 12:48:36
【问题描述】:

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

【问题讨论】:

  • 原来我需要为突出显示的状态设置不同的颜色。

标签: ios iphone uivisualeffectview


【解决方案1】:

您需要添加目标操作(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)
    }
}

【讨论】:

  • 太棒了!这很有帮助,因为我可能很快就会问自己这个问题。但实际上我正在寻找指示按钮被点击的动画。结果我需要为突出显示的状态设置不同的颜色
猜你喜欢
  • 2021-12-23
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多