ios UILabel 上的闪烁效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6224468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blinking effect on UILabel
提问by Abhinav
I have a UILabel with background color as grey.
我有一个背景颜色为灰色的 UILabel。
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?
任何线索如何实现这一目标?
采纳答案by Krishnabhadra
Use NSTimer
用 NSTimer
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0)
target:self
selector:@selector(blink)
userInfo:nil
repeats:TRUE];
BOOL blinkStatus = NO;
in your blink function
在你的眨眼功能中
-(void)blink{
if(blinkStatus == NO){
yourLabel.backgroundColor = [UIColor whiteColor];
blinkStatus = YES;
}else {
yourLabel.backgroundColor = [UIColor grayColor];
blinkStatus = NO;
}
}
回答by Flex Elektro Deimling
You can do this within a block:
您可以在一个块中执行此操作:
self.yourLabel.alpha = 1;
[UIView animateWithDuration:1.5 delay:0.5 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
self.yourLabel.alpha = 0;
} completion:nil];
So you dont need a second method.
所以你不需要第二种方法。
回答by Jaseem Abbas
Swift 3
斯威夫特 3
extension UILabel {
func startBlink() {
UIView.animate(withDuration: 0.8,
delay:0.0,
options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
animations: { self.alpha = 0 },
completion: nil)
}
func stopBlink() {
layer.removeAllAnimations()
alpha = 1
}
}
回答by tiguero
You can simply make an extension to the UILabelclass that will support the blinkingeffect. I don't think using a timer is a right approach since you won't have any fade effect.
您可以简单地对UILabel类进行扩展以支持闪烁效果。我不认为使用计时器是正确的方法,因为您不会有任何淡入淡出效果。
Here is the Swiftway to do this:
这是执行此操作的Swift方法:
extension UILabel {
func blink() {
self.alpha = 0.0;
UIView.animateWithDuration(0.8, //Time duration you want,
delay: 0.0,
options: [.CurveEaseInOut, .Autoreverse, .Repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
Swift 3:
斯威夫特 3:
extension UILabel {
func blink() {
self.alpha = 0.0;
UIView.animate(withDuration: 0.8, //Time duration you want,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
EDIT Swift 3: Works for almost any view
编辑 Swift 3:几乎适用于任何视图
extension UIView {
func blink() {
self.alpha = 0.0;
UIView.animate(withDuration: 0.8, //Time duration you want,
delay: 0.0,
options: [.curveEaseInOut, .autoreverse, .repeat],
animations: { [weak self] in self?.alpha = 1.0 },
completion: { [weak self] _ in self?.alpha = 0.0 })
}
}
回答by Chris
Rather use the view animations. It makes it very simple and is easy to control. Try this:
而是使用视图动画。它使它变得非常简单并且易于控制。尝试这个:
self.yourLabel.alpha = 1.0f;
[UIView animateWithDuration:0.12
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionRepeat |
UIViewAnimationOptionAutoreverse |
UIViewAnimationOptionAllowUserInteraction
animations:^{
self.yourLabel.alpha = 0.0f;
}
completion:^(BOOL finished){
// Do nothing
}];
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.
您可以调整值以获得不同的效果,例如,更改 animateWithDuration 将设置闪烁速度。此外,您可以在任何继承自 UIView 示例的按钮、标签、自定义视图等上使用它。
回答by Abhishek Bedi
A different approch but works. Blinking only for 3 seconds
一种不同的方法,但有效。仅闪烁 3 秒
extension UIView {
func blink() {
let animation = CABasicAnimation(keyPath: "opacity")
animation.isRemovedOnCompletion = false
animation.fromValue = 1
animation.toValue = 0
animation.duration = 0.8
animation.autoreverses = true
animation.repeatCount = 3
animation.beginTime = CACurrentMediaTime() + 0.5
self.layer.add(animation, forKey: nil)
}
}
回答by Deepak Samuel Rajan
Tweaking Krishnabhadra Answer to give a better blink effect
调整克里希那巴德拉答案以提供更好的眨眼效果
Declare a Class variable bool blinkStatus;
声明一个类变量 bool blinkStatus;
And paste code given below
并粘贴下面给出的代码
NSTimer *yourtimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(10.0 / 60.0) target:self selector:@selector(blink) userInfo:nil repeats:TRUE];
blinkStatus = FALSE;
-(void)blink{
if(blinkStatus == FALSE){
yourLabel.hidden=NO;
blinkStatus = TRUE;
}else {
yourLabel.hidden=YES;
blinkStatus = FALSE;
}
}
回答by DavidS
Got stuck when trying with swift and using multiple options but this seems to work nicely:
尝试使用 swift 并使用多个选项时卡住了,但这似乎很好用:
self.cursorLabel.alpha = 1
UIView.animate(withDuration: 0.7, delay: 0.0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
self.cursorLabel.alpha = 0
}, completion: nil)
回答by Alessandro Ornano
-(void) startBlinkingLabel:(UILabel *)label
{
label.alpha =1.0f;
[UIView animateWithDuration:0.32
delay:0.0
options: UIViewAnimationOptionAutoreverse |UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionBeginFromCurrentState
animations:^{
label.alpha = 0.0f;
}
completion:^(BOOL finished){
if (finished) {
}
}];
}
-(void) stopBlinkingLabel:(UILabel *)label
{
// REMOVE ANIMATION
[label.layer removeAnimationForKey:@"opacity"];
label.alpha = 1.0f;
}
回答by Edilson Junior
This is how it worked for me. I adapted the answer of @flex_elektro_deimling
这就是它对我的工作方式。我改编了@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.
第一个参数 UIView.animateWithDuration 是动画的总时间(在我的例子中我将它设置为 0.5),您可以在第一个和第二个(延迟)上设置不同的值来改变闪烁速度。
self.YOURLABEL.alpha = 0;
UIView.animateWithDuration(
0.5,
delay: 0.2,
options: UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse, animations: {
self.YOURLABEL.alpha = 1
},
completion:nil)