突出显示类似于 UIButton 的 UIView
我有一个带有多个子视图的 UIView 和一个可识别的关联点击手势,我想模仿它具有“触摸”效果。也就是说,当点击发生时,我想显示容器视图具有不同的背景颜色,并且任何子视图 UILabels 的文本也看起来突出显示。
当我从 UITapGestureRecognizer 收到点击事件时,我可以很好地更改背景颜色,甚至将 UILabel 设置为 [label setHighlighted:YES];
由于各种原因,我无法将 UIView 更改为 UIControl。
但是如果我添加一些 UIViewAnimation 来恢复突出显示,则什么也不会发生。有什么建议吗?
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
[label setHighlighted:YES]; // change the label highlight property
[UIView animateWithDuration:0.20
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[containerView setBackgroundColor:originalBgColor];
[label setHighlighted:NO]; // Problem: don't see the highlight reverted
} completion:^(BOOL finished) {
// nothing to handle here
}];
}
I have a UIView with a number of subviews and a Tap gesture recognized associated and I want to mimic it having a 'touch' effect. That is, when the tap happens, I want to show the container view to have a different background color and the text of any subview UILabels to also look highlighted.
When I receive the tap event from UITapGestureRecognizer, I can change the background color just fine and even set the UILabel to [label setHighlighted:YES];
For various reasons, I cannot change the UIView to UIControl.
But if I add some UIViewAnimation to revert the highlighting, nothing happens. Any suggestions?
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
[label setHighlighted:YES]; // change the label highlight property
[UIView animateWithDuration:0.20
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[containerView setBackgroundColor:originalBgColor];
[label setHighlighted:NO]; // Problem: don't see the highlight reverted
} completion:^(BOOL finished) {
// nothing to handle here
}];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Swift 4
您可以使用自定义视图,例如:
只需随意调整持续时间和动画即可。最后,您可以使用它来代替
UIView
,并且每当您单击它时,它都会更改其alpha
值,因此它看起来像一个突出显示。Swift 4
You can use a custom view like this for example:
Just play around with the duration and animations as you like. In the end you can use it instead of
UIView
and whenever you will click on it, it will change itsalpha
value so it will look like a highlight.setHighlighted 不是可动画化的视图属性。另外,您说的是两个相反的事情:您同时将突出显示设置为“是”和“否”。结果将是什么也没有发生,因为没有整体变化。
使用完成处理程序或延迟性能稍后更改突出显示。
编辑:
你说“都尝试过,但都不起作用”。也许您需要澄清我所说的延迟性能的含义。我刚刚尝试过这个,它工作得很好:
标签必须具有与其
highlightedTextColor
不同的textColor
,以便发生可见的事情。setHighlighted isn't an animatable view property. Plus, you're saying two opposite things: you setHighlighted to YES and NO in the same breath. The result will be that nothing happens because there's no overall change.
Use the completion handler or delayed performance to change the highlight later.
EDIT:
You say "tried both but neither worked." Perhaps you need clarification on what I mean by delayed performance. I just tried this and it works perfectly:
The label must have a different
textColor
vs itshighlightedTextColor
so that something visible happens.简单的解决方案是覆盖点击手势识别器
如下所示:
Swift 4.x
Simple solution is override tap gesture regognizer
like below:
Swift 4.x
斯威夫特 5.7
Swift 5.7
Swift 4 及以上版本
使用:
Swift 4 and above
use: