我有一个背景颜色为灰色的UILabel。
我想让这个标签有一个闪烁的效果,就像它应该变成一点白色,然后变成灰色,直到我在程序中将它关闭为止。
有什么方法可以实现这个效果吗?
我想让这个标签有一个闪烁的效果,就像它应该变成一点白色,然后变成灰色,直到我在程序中将它关闭为止。
有什么方法可以实现这个效果吗?
您可以在代码块内完成此操作:
self.yourLabel.alpha = 1;
[UIView animateWithDuration:1.5 delay:0.5 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
self.yourLabel.alpha = 0;
} completion:nil];
所以你不需要第二种方法。
[self.yourLabel.layer removeAllAnimations]
停止动画。 - dvs.alpha = 1;
,然后在动画块中设置 .alpha = 0;
,这样可以避免动画开始时出现奇怪的闪烁。 - Radu UrsacheSwift 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
}
}
您可以很简单地创建一个扩展 UILabel 类的方法,以支持闪烁效果。我认为使用计时器不是正确的方法,因为您将无法实现任何淡入淡出效果。
以下是实现此功能的 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:
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 })
}
}
编辑 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 })
}
}
使用NSTimer
NSTimer *timer = [NSTimer
scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0)
target:self
selector:@selector(blink)
userInfo:nil
repeats:TRUE];
BOOL blinkStatus = NO;
在您的闪烁函数中
-(void)blink{
if(blinkStatus == NO){
yourLabel.backgroundColor = [UIColor whiteColor];
blinkStatus = YES;
}else {
yourLabel.backgroundColor = [UIColor grayColor];
blinkStatus = NO;
}
}
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)
}
}
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
}];
您可以调整值以获得不同的效果,例如,更改animateWithDuration将设置闪烁速度。此外,您可以将其用于任何继承自UIView的内容,例如按钮、标签、自定义视图等。
-(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;
}
以下是基于Flex Elektro Deimling
的答案的我的Swift版本:
private func startTimeBlinkAnimation(start: Bool) {
if start {
timeContainerView.alpha = 1
UIView.animateWithDuration(0.6, delay: 0.3, options:[.Repeat, .Autoreverse], animations: { _ in
self.timeContainerView.alpha = 0
}, completion: nil)
}
else {
timeContainerView.alpha = 1
timeContainerView.layer.removeAllAnimations()
}
}
在尝试使用多个选项时,可能会遇到困难,但是以下方法似乎可以很好地解决:
self.cursorLabel.alpha = 1
UIView.animate(withDuration: 0.7, delay: 0.0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
self.cursorLabel.alpha = 0
}, completion: nil)
修改Krishnabhadra的答案以获得更好的闪烁效果
声明一个类变量bool blinkStatus;
并粘贴下面给出的代码
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;
}
}
UIView.animate(withDuration: 1, delay: 0.0, options: [.curveLinear, .repeat, .autoreverse], animations: { self.alpha = 1.0 }, completion: nil) }
} - Erik Peruzzi