UIPanGestureRecognizer在平移时停止工作,而不是在平移时停止工作

时间:2013-07-24 01:27:35

标签: ios opacity uipangesturerecognizer

我基本上是在这里创建一个屏幕调光器。我在屏幕上放置了一个黑色视图,并使用UIPanGestureRecognizer根据用户是向上还是向下滚动来调整不透明度。这是代码:

- (IBAction)dimScreen:(UIPanGestureRecognizer *)sender {
    //Get translation
    CGPoint translation = [sender translationInView:self.view];
    CGFloat distance = translation.y;

    //add a fraction of the translation to the alpha
    CGFloat newAlpha = self.blackScreen.alpha + distance/self.view.frame.size.height;
    //check if the alpha is more than 1 or less than 0
        if (newAlpha>1) {
        newAlpha = 1;
        }else if (newAlpha<0.0){
        newAlpha = 0.0;
        }
    self.blackScreen.alpha = newAlpha;
    //reset translation to get incremental change
    [sender setTranslation:CGPointZero inView:self.view];
}

如果我平移,不透明度变为1.0,我仍然可以调整它。如果我向上平移直到不透明度为0,我就不能再平底锅了。 dimScreen:选择器停止被调用。任何人都可以描述导致此问题的原因吗?

1 个答案:

答案 0 :(得分:1)

alpha值为0的视图不再接收触摸。事实上,似乎低于0.011 alpha,视图停止接收触摸。因此,可以通过更改else-if语句中的值来使代码工作:

if (newAlpha>1) {
        newAlpha = 1;
    }else if (newAlpha <= 0.011){
        newAlpha = 0.011;
    }

我见过其他人提到的值为0.02而不是0.011,这是我根据经验发现的。我不知道假设这些数字中的任何一个是固定的并且将来会继续发挥作用是多么“安全”,但它现在似乎有效。