Iphone UILabel带有触摸式发光按钮

Iphone UILabel带有触摸式发光按钮,iphone,ios,uibutton,uilabel,uigesturerecognizer,Iphone,Ios,Uibutton,Uilabel,Uigesturerecognizer,我有一个动态创建的标签列表,我正在为每个标签使用手势识别器来检测触摸/点击它们。我无法使用UIButton,因为我想将每个标签特有的一些文本传递给触摸事件处理程序,UIButton不允许将文本作为userinfo传递。我无法使用UIButton.tag传递附加信息 现在我想在我的UIlabel上的触摸上显示UIButton一样的发光效果。如果有其他方法通知用户触摸了标签,也可以。我一直在考虑使用某种快速动画或抖动效果。有什么想法或解决办法吗 提前感谢。为什么不以编程方式创建按钮,并将按钮类型设置

我有一个动态创建的标签列表,我正在为每个标签使用手势识别器来检测触摸/点击它们。我无法使用UIButton,因为我想将每个标签特有的一些文本传递给触摸事件处理程序,UIButton不允许将文本作为userinfo传递。我无法使用UIButton.tag传递附加信息

现在我想在我的UIlabel上的触摸上显示UIButton一样的发光效果。如果有其他方法通知用户触摸了标签,也可以。我一直在考虑使用某种快速动画或抖动效果。有什么想法或解决办法吗


提前感谢。

为什么不以编程方式创建按钮,并将按钮类型设置为自定义。像这样-

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];
这里我们定义了按钮的外观。您可以定义UIControlStateHighlighted的外观

我想说的是,使用uibuttons,你可以得到你需要的东西。不必使用UILabel

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)

我现在用的是UIButton而不是UILabel。由于我无法通过UIButton的addTarget消息传递额外的参数,现在我维护了UIButton.tag的映射,以显示按下按钮时所需的其他信息。现在似乎可以用了,我可以使用UIButton.ok的所有功能,但是如果你想让uilabel表现得像UIButton。那么最好使用uibutton!不管怎样只要它有效。。。很高兴我能分享我所知道的。。。