iOS 会呼吸的动画

1.思路:呼吸灯动画实现就是设置元件的透明度从无到有一直循环。

2.代码实现

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *myView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _myView.layer.cornerRadius = 8;

//添加动画效果,aAlpha是给此次动画添加的标识,方便我们删除动画时,根据这个标识删除。

    [_myView.layer addAnimation:[self alphaLight:0.5] forKey:@"aAlpha"];

}

#pragma mark - 呼吸灯动画

-(CABasicAnimation *)alphaLight:(float)time

{

    CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"];

    animation.fromValue = [NSNumber numberWithFloat:1.0f];

    animation.toValue = [NSNumber numberWithFloat:0.3f];//这是透明度。

    animation.autoreverses = YES;

    animation.duration = time;

    animation.repeatCount = MAXFLOAT;

    animation.removedOnCompletion = NO;

    animation.fillMode = kCAFillModeForwards;

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    return animation;

}

3.移除动画

[_myView.layer removeAnimationForKey:@"aAlpha"];


猜你喜欢

转载自blog.csdn.net/huanghaiyan_123/article/details/52788016