当其 alpha 为 0 时,iOS`UIView` 停止响应手势识别器? [英] iOS `UIView` stops responding to gesture recognizer when its alpha is 0?

查看:108
本文介绍了当其 alpha 为 0 时,iOS`UIView` 停止响应手势识别器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UIView,它的行为是这样的:当我从 nib 加载它并将它添加到我的视图层次结构后,它首先几乎是透明的(alpha = 0.1),当我点击时它,它变得不透明(alpha = 1.0),一段时间后,它自动变得几乎透明(alpha = 0.1).

I have a customized UIView, its behavior is like this: after I load it from nib and add it to my view hierarchy, It firstly is almost transparent (alpha = 0.1), when I tap it, it becomes opaque (alpha = 1.0), after some time, it automatically becomes almost transparent (alpha = 0.1).

自定义视图中的代码是这样的,它的工作原理和我上面描述的一样:

The code in the customized view is like this, it works just as I described above:

- (void)awakeFromNib {
  [self setup];
}

- (void)setup {
  self.alpha = 0.1f;
  [self addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                 initWithTarget:self
                                         action:@selector(tapped:)]];
}

- (void)tapped:(UITapGestureRecognizer *)tapRecognizer {
  if (self.alpha == 1.0) {
    [self hideSelf];
  } else {
    [UIView animateWithDuration:0.5
        animations:^{ self.alpha = 1.0f; }
        completion:^(BOOL finished) {
            [self.timer invalidate];
            self.timer = nil;
            self.timer = [NSTimer timerWithTimeInterval:3
                                                 target:self
                                               selector:@selector(hideSelf)
                                               userInfo:nil
                                                repeats:NO];
            [[NSRunLoop currentRunLoop] addTimer:self.timer
                                         forMode:NSDefaultRunLoopMode];
        }];
  }
}

- (void)hideSelf {
  [UIView animateWithDuration:0.5
      animations:^{ self.alpha = 0.1f; }
      completion:^(BOOL finished) {
          [self.timer invalidate];
          self.timer = nil;
      }];
}

但我不想要几乎透明(alpha = 0.1)",我想要透明(alpha = 0.0)".所以我只是将代码中的0.1"更改为0.0".但是当我点击视图时,它甚至不会调用 tapped: 方法.为什么会这样?我怎样才能让它工作?

But I don't want "almost transparent (alpha = 0.1)", I want "transparent (alpha = 0.0)". So I simply change the "0.1" to "0.0" in my code. But when I tap on the view, it doesn't even call the tapped: method. Why is this so? How can I make it work?

推荐答案

它是这样工作的,如果您将 alpha 视图更改为零,它会停止获取触摸事件.

It works that way, if you change the alpha view to zero it stops getting touch events.

您可以将视图的背景颜色更改为透明,而不是更改视图的 alpha,这样您的视图将不可见并且您会收到事件.

You could though change view's background color to transparent, instead of changing view's alpha, that way your view won't be visible and you get the events.

这篇关于当其 alpha 为 0 时,iOS`UIView` 停止响应手势识别器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆