简体   繁体   English

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

[英]How to detect gesture on a UIView after it becomes transparent?

I have a UIView on top of some other UI components to detect a long-pressed gesture. 我在其他一些UI组件之上有一个UIView来检测长按手势。 When a long press begins, I want to tip the user by changing the background color to gray & alpha = 0.1. 长按开始时,我想通过将背景颜色更改为灰色和alpha = 0.1来向用户提示。

After the long press ends, the UIView has to be changed back as completely transparent. 长按结束后,必须将UIView重新更改为完全透明。 I set its alpha to 0, but the problem is ... 我将其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变回其原始状态以便在最初创建时可以检测到更多手势的正确方法是什么?

Setting a UIView's alpha property to 0 will make it stop receiving touches. 将UIView的alpha属性设置为0将使其停止接收触摸。 Instead, set its background to UIColor.clearColor() when you want it invisible. 而是在不希望显示背景时将其背景设置为UIColor.clearColor()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM