iOS `UIView` stops responding to gesture recognizer when its alpha is 0?

ぐ巨炮叔叔 提交于 2020-12-08 06:36:53

问题


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;
      }];
}

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?


回答1:


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

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.




回答2:


fully transparent means hidden right rather than making it to 0.0 make the UIview hide and place a button with Uicolor clear color at the same point and transfer control to that.



来源:https://stackoverflow.com/questions/26293470/ios-uiview-stops-responding-to-gesture-recognizer-when-its-alpha-is-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!