我有一个动态创建的标签列表,我对每个标签使用GestureRecognizer来检测触摸/点击它们。我不能使用UIButton,因为我想传递一些对每个标签唯一的文本给触摸事件处理器,而UIButton不允许文本作为userinfo传递。我不能使用UIButton.tag传递附加信息。
现在我想在我的UIlabel上实现类似UIButton的发光效果。如果有其他方式通知用户标签被触摸了,也可以这样做。我还在考虑使用某种快速动画或抖动效果。有什么想法或变通办法吗?
提前谢谢。
发布于 2011-10-17 11:08:47
为什么不以编程方式创建按钮,并将按钮类型设置为自定义。就像这样-
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];这里我们已经定义了按钮应该是什么样子的,UIControlStateNormal。您可以为UIControlStateHighlighted定义它应该是什么样子。
我想说的是,使用to按钮你可以得到你需要的东西。不需要使用uilabels。
发布于 2018-06-06 19:50:46
Swift 4
1-使用动画创建UIView扩展
2-在文本字段、按钮、、视图(UIView的任何子类)上使用它
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")
}
}使用方法:
//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)https://stackoverflow.com/questions/7788978
复制相似问题