自定义动画CABasicAnimation

来源:互联网 发布:电脑双肩包 知乎 编辑:程序博客网 时间:2024/06/05 12:42
#import "TestViewController.h"@interface TestViewController ()@property (nonatomic, strong)NSNumber *index;@property (nonatomic ,strong)NSMutableArray *recycleArray;@end@implementation TestViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor whiteColor];    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];//控制开关按钮    btn.top = 70    ;    btn.left =70;    [btn addTarget:self action:@selector(test2) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];    [self test1];}- (void)test1{    _recycleArray = [NSMutableArray array];    CGPoint FirstPonit = CGPointMake(self.view.centerX, self.view.centerY-20);    NSArray *arraySize = @[NSStringFromCGSize(CGSizeMake(self.view.width/4, self.view.width/4)),NSStringFromCGSize(CGSizeMake(60, 60)),NSStringFromCGSize(CGSizeMake(40,40 )),NSStringFromCGSize(CGSizeMake(20, 20)),NSStringFromCGSize(CGSizeMake(80, 80))];    NSArray *arrayPoint = @[NSStringFromCGPoint(FirstPonit),NSStringFromCGPoint(CGPointMake(FirstPonit.x+90, FirstPonit.y-70)),NSStringFromCGPoint(CGPointMake(FirstPonit.x-90 , FirstPonit.y-95)),NSStringFromCGPoint(CGPointMake(FirstPonit.x+90 , FirstPonit.y+90)),NSStringFromCGPoint(CGPointMake(FirstPonit.x-90 , FirstPonit.y+90))];    for (int i = 0; i<5; i++) {        CGSize size = CGSizeFromString(arraySize[i]);        CGPoint point = CGPointFromString(arrayPoint[i]);        UIView *view = [[UIView alloc] init];        view.size = size;        view.centerX = point.x;        view.centerY = point.y;        view.layer.cornerRadius = view.width/2;        switch (i) {            case 0:            {                view.backgroundColor = [UIColor redColor];            }                break;            case 1:            {                view.backgroundColor = [UIColor grayColor];            }                break;            case 2:            {                view.backgroundColor = [UIColor greenColor];            }                break;            case 3:            {                view.backgroundColor = [UIColor orangeColor];            }                break;            case 4:            {                view.backgroundColor = [UIColor blackColor];            }                break;            default:                break;        }        [self.recycleArray addObject:view];        [self changeTheView:view Point:CGPointFromString(arrayPoint[i])];    }}- (void)changeTheView:(UIView*)view Point:(CGPoint)point{    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    animation.fromValue = [NSNumber numberWithFloat:0.01];    animation.toValue  = [NSNumber numberWithFloat:1];    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];    move.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.view.width/2, self.view.height/2-20)];    move.toValue = [NSValue valueWithCGPoint:point];    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.1];    opacityAnimation.toValue = [NSNumber numberWithFloat:1.0];    CAAnimationGroup *group = [CAAnimationGroup animation];    group.duration = 1;//它设定开始值到结束值花费的时间    group.repeatCount = 1;//默认的是 0,意味着动画只会播放一次。如果指定一个无限大的重复次数,使用 1e100f    group.autoreverses = NO;//当你设定这个属性为 YES 时,在它到达目的地之后,动画的返回到开始的值,代替了直接跳转到 开始的值。    group.removedOnCompletion = NO;    group.fillMode = kCAFillModeForwards;    group.animations = @[animation,move,opacityAnimation];    [view.layer addAnimation:group forKey:@"aaa"];}- (void)test2{    self.index = 0;}-(void)setIndex:(NSNumber*)index{    _index = index;    NSInteger time = [index integerValue];    if (time <5) {        dispatch_async(dispatch_get_main_queue(), ^{            [self.view addSubview:_recycleArray[time]];        });        time ++;        [self performSelector:@selector(setIndex:) withObject:[NSNumber numberWithInteger:time] afterDelay:1];//每一秒调用方法    }}
0 0
原创粉丝点击