访次: AmazingCounters.com 次

呼吸灯

吸吸灯动绘完成便是设置元件的通明度从无到有不停轮回。 2.代码完成 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) ]

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"];

posted @ 2016-11-04 16:09  JusDoit  阅读(264)  评论(0编辑  收藏  举报