UILabel 上的闪烁效果
我有一个背景颜色为灰色的 UILabel。
我想要这个标签上有闪烁效果,就像它应该变得有点白色和白色一样。然后变成灰色,它应该一直发生,直到我以编程方式将其关闭。
有任何线索如何实现这一目标吗?
I have a UILabel with background color as grey.
I want a blinking effect on this label like it should become a little white & then become gray and it should keep happen till I turn it off programatically.
Any clue how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您可以在一个块内执行此操作:
因此您不需要第二种方法。
You can do this within a block:
So you dont need a second method.
斯威夫特3
Swift 3
您只需对 UILabel 类进行扩展即可支持闪烁效果。我认为使用计时器不是正确的方法,因为不会有任何淡入淡出效果。
以下是执行此操作的Swift方法:
Swift 3:
编辑 Swift 3:适用于几乎任何视图
You can simply make an extension to the UILabel class that will support the blinking effect. I don't think using a timer is a right approach since you won't have any fade effect.
Here is the Swift way to do this:
Swift 3:
EDIT Swift 3: Works for almost any view
使用 NSTimer
在眨眼函数中
Use
NSTimer
in your blink function
一种不同的方法但有效。仅闪烁 3 秒
A different approach but works. Blinking only for 3 seconds
而是使用视图动画。它使它非常简单并且易于控制。试试这个:
您可以调整值以获得不同的效果,例如,更改 animateWithDuration 将设置闪烁速度。此外,您可以在继承自 UIView 的任何内容上使用它,例如按钮、标签、自定义视图等。
Rather use the view animations. It makes it very simple and is easy to control. Try this:
You can tweak the values to get different effects for example, changing animateWithDuration wil set the blinking speed. Further you can use it on anything that inherits from UIView example a button, label, custom view etc.
调整 Krishnabhadra Answer 以提供更好的眨眼效果
声明一个类变量
bool flashStatus;
并粘贴下面给出的代码
Tweaking Krishnabhadra Answer to give a better blink effect
Declare a Class variable
bool blinkStatus;
And paste code given below
我的快速版本基于
Flex Elektro Deimling 的
答案:My swift version based on
Flex Elektro Deimling's
answer:在尝试使用 swift 并使用多个选项时陷入困境,但这似乎效果很好:
Got stuck when trying with swift and using multiple options but this seems to work nicely:
这对我来说就是这样的。我改编了@flex_elektro_deimling的答案
第一个参数UIView.animateWithDuration是动画的总时间(在我的例子中我将其设置为0.5),您可以在第一个和第二个(延迟)上设置不同的值来改变闪烁速度。
This is how it worked for me. I adapted the answer of @flex_elektro_deimling
First parameter UIView.animateWithDuration is the total time of the animation (In my case I've set it to 0.5), you may set different values on the first and second (delay) to change the blinking speed.
}
}
这是我在 Swift 4.0 中的解决方案,具有任何 UIVIew 的扩展
Here is my solution in Swift 4.0 with extension for any UIVIew
对于 Swift 3+,在此处所有出色答案的基础上,我最终进行了一些调整,为我提供了平滑的闪烁效果,在给定的周期数后自动停止。
For Swift 3+, building on all the great answers here, I ended up with a few tweaks that gave me smooth blinking effect that automatically stops after a given number of cycles.