更新swift方法:
//呼吸灯
func AlphaLight(time: CGFloat) -> CABasicAnimation {
let animation = CABasicAnimation.init(keyPath: "opacity")
animation.fromValue = 1
animation.toValue = 0
animation.autoreverses = true
animation.duration = CFTimeInterval(time)
animation.repeatCount = 1000
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction.init(name: .easeIn)
return animation
}
//增加呼吸效果
tipL.layer.add( AlphaLight(time: 1), forKey: "aAlpha")
//去除呼吸效果
pL.layer.removeAnimation(forKey: "aAlpha")
以前oc方法
头文件#import <QuartzCore/QuartzCore.h>
+(CABasicAnimation *) AlphaLight:(float)time
{
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。
animation.autoreverses = YES;
animation.duration = time;//动画循环的时间,也就是呼吸灯效果的速度
animation.repeatCount = MAXFLOAT;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
return animation;
}
//给指定元件添加动效:[yourUIView.layer addAnimation:[self AlphaLight:0.5] forKey:@"aAlpha"];
//移除动画:[_myView.layer removeAnimationForKey:@"aAlpha"];
原文地址:[http://v2it.win/ios/ios%E4%BC%9A%E5%91%BC%E5%90%B8%E7%9A%84%E5%8A%A8%E7%94%BB/](http://v2it.win/ios/ios%E4%BC%9A%E5%91%BC%E5%90%B8%E7%9A%84%E5%8A%A8%E7%94%BB/)
相关推荐(动画效果):http://blog.jobbole.com/69111/