I can't figure this out, and I don't think this really explains it either.
I have a UILabel
that can be tapped by the user to hide or show it, set up like this:
self.numberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(hideOrShowNumber)];
[self.numberLabel addGestureRecognizer:tapGesture];
I would like to animate the hiding and showing of the label by setting the alpha
value on the UILabel
. However, if I set the alpha value to 0.0f
, the label no longer accepts taps, so even if the user can hide the label, she can't show it anymore!
My workaround goes like this:
When hiding the label: - Animate the alpha value to 0.0f. - Set the text color of the label to black (which makes it invisible because the background is black) - Reset the alpha to 1.0f.
When showing the label: - Set the alpha to 0.0f (because it was left at 1.0f when the label was hidden). - Set the text color to another color than black (depending on game state). - Animate the alpha value to 1.0f.
The code looks like this (there are some state variables included, but self.numberLabel
is the reference to the UILabel
):
NSTimeInterval duration = 0.6f;
if (self.numberIsVisible) {
[UIView animateWithDuration:duration
animations:^{
self.numberLabel.alpha = 0.0f;
}
completion:^(BOOL done) {
self.numberLabel.textColor = [UIColor blackColor];
self.numberLabel.alpha = 1.0f;
}
];
self.numberIsVisible = NO;
}
else {
UIColor *rightColor = [UIColor whiteColor];
if ([GameState sharedGameState].haveMatch) {
rightColor = [UIColor colorWithRed:0.0/255.0 green:127.0/255.0 blue:255.0/255.0 alpha:1.0];
}
self.numberLabel.alpha = 0.0f;
self.numberLabel.textColor = rightColor;
[UIView animateWithDuration:duration
animations:^{
self.numberLabel.alpha = 1.0f;
}
];
self.numberIsVisible = YES;
}
It works, but it is a little clunky.
So the question is, why does setting the transparency of the UILabel
make it lose user interaction? Is this by design, and is it documented somewhere? I can't find anything about this in the UIGestureRecognizer
docs.