如何在UIView变透明后检测手势?

时间:2021-04-11 20:00:44

I have a UIView on top of some other UI components to detect a long-pressed gesture. When a long press begins, I want to tip the user by changing the background color to gray & alpha = 0.1.

我在其他一些UI组件上面有一个UIView来检测一个长按的手势。当长按开始时,我想通过将背景颜色更改为灰色&alpha = 0.1来提示用户。

After the long press ends, the UIView has to be changed back as completely transparent. I set its alpha to 0, but the problem is ...

长按结束后,UIView必须更换为完全透明。我把它的alpha设置为0,但问题是......

no further guesture can be detected.

无法再检测到其他人。

mainView = UIView()
mainView.frame = ...
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action:Selector("longPressed:"))
mainView.addGestureRecognizer(longPressRecognizer)

func longPressed(sender: UILongPressGestureRecognizer) {
    let view = sender.view!

    if sender.state == .Began {
        view.backgroundColor = UIColor.grayColor()
        view.alpha = 0.1
    } else if (sender.state == .Ended || sender.state == .Cancelled || sender.state == .Failed) {
        view.backgroundColor = UIColor.whiteColor()
        view.alpha = 0
    }
}

What's the correct way to make this UIView changed back to its original state so that further gestures can be detected as it's initially created?

什么是使这个UIView变回其原始状态的正确方法,以便在最初创建时可以检测到更多手势?

1 个解决方案

#1


6  

Setting a UIView's alpha property to 0 will make it stop receiving touches. Instead, set its background to UIColor.clearColor() when you want it invisible.

将UIView的alpha属性设置为0将使其停止接收触摸。相反,当您希望它不可见时,将其背景设置为UIColor.clearColor()。

#1


6  

Setting a UIView's alpha property to 0 will make it stop receiving touches. Instead, set its background to UIColor.clearColor() when you want it invisible.

将UIView的alpha属性设置为0将使其停止接收触摸。相反,当您希望它不可见时,将其背景设置为UIColor.clearColor()。