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.
From the official doc (Regulating Touch Event Delivery section)
Turning off delivery of touch events. By default, a view receives touch events, but you can set its userInteractionEnabled property to NO to turn off delivery of touch events. A view also does not receive these events if it’s hidden or if it’s transparent.
Having full transparency (alpha = 0) on a view is consider to be similar as having a view hidden so there is no reason the user interaction should he handled in that case. You could try to have nearly-transparent UILabel
instead. An alpha of 0.1
seems to be the limit.
It seems as though any UIView with an alpha of < 0.1f acts as though the hidden property were set to YES. In other words, since your view is transparent, it will not receive touch events. See this post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With