Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 876
axl411 Avatar asked Oct 10 '14 06:10

axl411


1 Answers

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.

like image 147
cris Avatar answered Oct 11 '22 14:10

cris