[英]UILabel with UIButton like glow on touch
I have a dynamically created list of labels and I am using GestureRecognizer
for each of the labels to detect touches/click on them. 我有一个动态创建的标签列表,并且我对每个标签使用
GestureRecognizer
来检测触摸/单击它们。 I couldn't use UIButton
as I wanted to pass some text which is unique to each label to the touch event handler and UIButton
would not allow text to be passed as userinfo
. 我无法使用
UIButton
因为我想将每个标签唯一的一些文本传递给触摸事件处理程序,并且UIButton
不允许将文本作为userinfo
传递。 I cannot use UIButton.tag
to pass the additional info. 我无法使用
UIButton.tag
传递附加信息。
Now I want the UIButton
like glow effect on touch on my UIlabel
. 现在我想让
UIButton
像在UIlabel
上触摸时一样发光效果。 If there are other ways to notify a user that a label was touched, that work too. 如果还有其他方法可以通知用户标签被触摸,则该方法也可以。 I was alo thinking of using some kind of quick animation or jiggling effect.
我当时还在考虑使用某种快速动画或微动效果。 Any ideas or workarounds?
有什么想法或解决方法吗?
Thanks in advance. 提前致谢。
Swift 4 斯威夫特4
1- Create UIView extension with the animation 1-用动画创建UIView扩展
2- Use it on textfields, buttons , views (any subclass of UIView) 2-在文本字段, 按钮 ,视图(UIView的任何子类)上使用它
UIView extension UIView扩展
import UIKit
extension UIView{
enum GlowEffect:Float{
case small = 0.4, normal = 1, big = 5
}
func doGlowAnimation(withColor color:UIColor, withEffect effect:GlowEffect = .normal) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowRadius = 0
layer.shadowOpacity = effect.rawValue
layer.shadowOffset = .zero
let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
glowAnimation.fromValue = 0
glowAnimation.toValue = 1
glowAnimation.beginTime = CACurrentMediaTime()+0.3
glowAnimation.duration = CFTimeInterval(0.3)
glowAnimation.fillMode = kCAFillModeRemoved
glowAnimation.autoreverses = true
glowAnimation.isRemovedOnCompletion = true
layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
}
}
How to use it: 如何使用它:
//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
Why dont you create buttons programmatically and make the button type as custom. 为什么不以编程方式创建按钮并使按钮类型自定义。 Like so -
像这样-
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton setTitle:@"My Button"];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Here we have defined what the button should look like UIControlStateNormal
. 在这里,我们定义了按钮应为
UIControlStateNormal
外观。 You can define what it should look like for UIControlStateHighlighted
. 您可以定义
UIControlStateHighlighted
外观。
What i want to say is that using uibuttons you can get what you need. 我想说的是,使用uibutton可以得到所需的东西。 Dont have to use uilabels.
不必使用uilabels。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.