iOS`UIView`在alpha为0时停止响应手势识别器?

axl*_*411 7 transparency objective-c ios

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

自定义视图中的代码是这样的,它就像我上面描述的那样工作:

- (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;
      }];
}
Run Code Online (Sandbox Code Playgroud)

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

cri*_*ris 9

它以这种方式工作,如果您将Alpha视图更改为零,它将停止获取触摸事件.

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