axl411
axl411

Reputation: 930

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?

Upvotes: 8

Views: 2575

Answers (2)

cris
cris

Reputation: 333

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.

Upvotes: 15

Mini
Mini

Reputation: 213

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.

Upvotes: 1

Related Questions