首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有类似UIButton的glow on touch的UILabel

具有类似UIButton的glow on touch的UILabel
EN

Stack Overflow用户
提问于 2011-10-17 11:04:16
回答 2查看 1.9K关注 0票数 0

我有一个动态创建的标签列表,我对每个标签使用GestureRecognizer来检测触摸/点击它们。我不能使用UIButton,因为我想传递一些对每个标签唯一的文本给触摸事件处理器,而UIButton不允许文本作为userinfo传递。我不能使用UIButton.tag传递附加信息。

现在我想在我的UIlabel上实现类似UIButton的发光效果。如果有其他方式通知用户标签被触摸了,也可以这样做。我还在考虑使用某种快速动画或抖动效果。有什么想法或变通办法吗?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-17 11:08:47

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

代码语言:javascript
复制
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。

票数 0
EN

Stack Overflow用户

发布于 2018-06-06 19:50:46

Swift 4

1-使用动画创建UIView扩展

2-在文本字段、按钮、、视图(UIView的任何子类)上使用它

UIView扩展

代码语言:javascript
复制
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")
    }
}

使用方法:

代码语言:javascript
复制
//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7788978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档