我无法弄清楚这一点,我也不认为this https://stackoverflow.com/questions/11142176/view-transparency-and-gesture-handling也确实解释了这一点。
我有一个UILabel
用户可以点击来隐藏或显示它,设置如下:
self.numberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(hideOrShowNumber)];
[self.numberLabel addGestureRecognizer:tapGesture];
我想通过设置来动画标签的隐藏和显示alpha
值于UILabel
。但是,如果我将 alpha 值设置为0.0f
,标签不再接受点击,因此即使用户可以隐藏标签,她也无法再显示它!
我的解决方法是这样的:
隐藏标签时:
- 将 alpha 值设置为 0.0f。
- 将标签的文本颜色设置为黑色(这使得它不可见,因为背景是黑色的)
- 将 alpha 重置为 1.0f。
显示标签时:
- 将 alpha 设置为 0.0f(因为隐藏标签时它保留为 1.0f)。
- 将文本颜色设置为黑色以外的其他颜色(取决于游戏状态)。
- 将 alpha 值设置为 1.0f。
代码如下所示(包含一些状态变量,但是self.numberLabel
是对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;
}
它可以工作,但有点笨重。
所以问题是,why是否设置透明度UILabel
让它失去用户交互?这是设计使然吗?它是否记录在某处?我在 中找不到任何关于此的信息UIGestureRecognizer
docs.