Animation

动画

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //CALayer
    self.sunView.layer.backgroundColor = [UIColor blackColor].CGColor;
    //切圆角
    self.sunView.layer.cornerRadius = 100;
    //设置阴影的透明度
    self.sunView.layer.shadowOpacity = 1;
    //设置阴影颜色
    self.sunView.layer.shadowColor = [UIColor orangeColor].CGColor;
    //设置阴影偏移量
    self.sunView.layer.shadowOffset= CGSizeMake(1, 1);
    //设置模糊程度
    self.sunView.layer.shadowRadius = 50;
    //设置边框颜色
    self.sunView.layer.borderColor = [UIColor yellowColor].CGColor;
    //设置边框大小
    self.sunView.layer.borderWidth = 2;
}


//使用上下文完成动画
- (IBAction)contextAnimation:(id)sender {
    //1.创建动画
    //第一个参数 给动画起一个名字
    //第二个参数 为哪一个视图做的动画
    [UIView beginAnimations:@"myAnimation" context:_heardView];
    //2.配置动画
    //设置动画时间
    [UIView setAnimationDuration:0.8];
    //设置是否重复
    [UIView setAnimationRepeatAutoreverses:YES];
    //设置重复次数
    [UIView setAnimationRepeatCount:50];
    //设置动画延迟时间
    [UIView setAnimationDelay:3];
    //设置视图最终状态
    self.heardView.center = CGPointMake(50, 50);
    
    //设置代理
    [UIView setAnimationDelegate:self];
    //动画开始时 做什么
    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
    //动画结束时做什么
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    //3.提交动画
    [UIView commitAnimations];
    
}
//动画开始
-(void)animationWillStart:(NSString *)animationID context:(void *)context
{
    NSLog(@"动画开始啦");
    NSLog(@"%@",context);
}
//动画结束
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"动画结束了");
    //将试图回归到起始位置
    self.heardView.center = self.lastCenter;
}
//使用Block完成动画
//平移
- (IBAction)translationAnimation:(id)sender {
    //最简单的语法块
//    [UIView animateWithDuration:1 animations:^{
//        self.heardView.transform = CGAffineTransformTranslate(self.heardView.transform, 100, 0);
//    }];
    //移除动画
    [self.heardView.layer removeAllAnimations];
    
    [UIView animateWithDuration:1 animations:^{
        //平移
        self.heardView.transform = CGAffineTransformTranslate(self.heardView.transform, 200, 0);
    } completion:^(BOOL finished) {
        //平移东湖结束之后 做什么
        [UIView animateWithDuration:1 animations:^{
            //self.heardView.transform = CGAffineTransformTranslate(self.heardView.transform, -200, 0);
            self.heardView.transform = _transform;
        }];
    }];
    
    
}
//旋转
- (IBAction)rotationAnimation:(id)sender {
    
    //移除动画
    [self.heardView.layer removeAllAnimations];
    
    [UIView animateWithDuration:1 animations:^{
        //旋转
        self.heardView.transform = CGAffineTransformRotate(self.heardView.transform, M_PI);
    } completion:^(BOOL finished) {
        //旋转
        [UIView animateWithDuration:1 animations:^{
            //self.heardView.transform = CGAffineTransformRotate(self.heardView.transform, M_PI);
            self.heardView.transform = _transform;
        }];
    }];
    
}
//缩放
- (IBAction)scaleAnimation:(id)sender {
    
    //移除动画
    [self.heardView.layer removeAllAnimations];
    
    [UIView animateWithDuration:0.3 delay:0 options:(UIViewAnimationOptionRepeat) animations:^{
        self.heardView.transform = CGAffineTransformScale(self.heardView.transform, 2, 2);
    } completion:^(BOOL finished) {
        //self.heardView.transform = CGAffineTransformScale(self.heardView.transform, 0.5, 0.5);
        //移除动画
        [self.heardView.layer removeAllAnimations];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.heardView.transform = _transform;
            
            
        }];
    }];
    
}
//过渡
- (IBAction)transitionAnimation:(id)sender {
    
    UIView *aView = [[UIView alloc] initWithFrame:self.heardView.bounds];
    aView.backgroundColor = [UIColor redColor];
    [self.heardView addSubview:aView];
    
    UIView *bView = [[UIView alloc] initWithFrame:self.heardView.bounds];
    bView.backgroundColor = [UIColor greenColor];
    [self.heardView addSubview:bView];
    
    //过渡动画
    [UIView transitionFromView:bView toView:aView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom completion:^(BOOL finished) {
        [UIView transitionFromView:aView toView:bView duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
            
        }];
    }];
    
    
}

- (IBAction)CABasicAnimation:(id)sender {
    //CABasicAnimation 有两个重要的属性
    //fromValue 初始值  toValue目标值
    //创建动画
    //参数的作用: layer的属性字符串形式
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //配置动画
    //动画时间
    animation.duration = 5.0f;
    animation.repeatCount = 10;
    //动画的初始值
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
    //动画的目标值
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(375, 200)];
    //添加动画
    [self.sunView.layer addAnimation:animation forKey:@"position"];
    
    
    //改变颜色
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    //配置动画
    animation2.duration = 1.0f;
    animation2.repeatCount = 100;
    
    //设置初始值
    animation2.fromValue = (id)[UIColor redColor].CGColor;
    //目标值
    animation2.toValue = (id)[UIColor yellowColor].CGColor
    ;
    
    //添加动画
    [self.sunView.layer addAnimation:animation2 forKey:@"backgroundColor"];
    
    
}

- (IBAction)CAKeyPathAnimation:(id)sender {
    
    //CAKeyPathAnimation 有一个重要的属性 path
    //创建动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //创建路径
    CGMutablePathRef path = CGPathCreateMutable();
    //添加起点
    CGPathMoveToPoint(path, NULL, 0, 0);
    
//    //添加关键路径点
//    CGPathAddLineToPoint(path, NULL, 375, 0);
//    CGPathAddLineToPoint(path, NULL, 375, 667);
//    CGPathAddLineToPoint(path, NULL, 0, 667);
//    CGPathAddLineToPoint(path, NULL, 0, 0);
//    CGPathAddLineToPoint(path, NULL, self.sunView.center.x, self.sunView.center.y);
    
    CGFloat r = 375 / 2;
    CGPathMoveToPoint(path, NULL, 0,  375 / 2 + 100);
    for (float x = 1; x <375; x++) {
        
        //获取y
        float y = - sqrtf(r * r - (x - r)*(x - r)) + r + 100;
        
        CGPathAddLineToPoint(path, NULL, x, y);
    }
    
    for (float x = 374; x > 0; x--) {
        
        //获取y
        float y =  sqrtf(r * r - (x - r)*(x - r)) + r + 100;
        
        CGPathAddLineToPoint(path, NULL, x, y);
    }
    
 
    //设置路径
    animation.path = path;
    
    //设置时间
    animation.duration = 10.0f;
    
    //添加动画
    [self.sunView.layer addAnimation:animation forKey:@"position"];
    
}

- (IBAction)CAValuesAnimation:(id)sender {
    
    //CAKeyFrameAnimation 关键属性 values
    //创建动画
    CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //配置动画
    animation1.duration = 10;
    NSMutableArray *values = [NSMutableArray array];
    //获取半径
    CGFloat r = 375 / 2;
    
    //半圆
    for (int x = 0; x < 375; x++) {
        //获取y
        float y = -sqrt(r * r - (x - r)*(x - r)) + r + 150;
        //创建点对象
        NSValue *value = [NSValue valueWithCGPoint:CGPointMake(x, y)];
        
        //放入数组
        [values addObject:value];
    }
    
    animation1.values =values;
    //添加动画
    [self.sunView.layer addAnimation:animation1 forKey:@"position"];
    
    //改变颜色
    CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    //配置动画
    //动画持续时间
    animation2.duration = 10.0f;
    
    //配置值数组
    animation2.values = @[(id)[UIColor orangeColor].CGColor,(id)[UIColor whiteColor].CGColor,(id)[UIColor orangeColor].CGColor];
    
    //添加动画
    [self.sunView.layer addAnimation:animation2 forKey:@"backgroundColor"];
    
}

- (IBAction)CAAnimationGroup:(id)sender {
    
    //创建动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    //创建动画1
    CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    NSMutableArray *values = [NSMutableArray array];
    //获取半径
    CGFloat r = 375 / 2;
    
    //半圆
    for (int x = 0; x < 375; x++) {
        //获取y
        float y = -sqrt(r * r - (x - r)*(x - r)) + r + 150;
        //创建点对象
        NSValue *value = [NSValue valueWithCGPoint:CGPointMake(x, y)];
        
        //放入数组
        [values addObject:value];
    }
    
    animation1.values = values;
    
    
    //创建动画2
    CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    animation2.values = @[(id)[UIColor orangeColor].CGColor,(id)[UIColor whiteColor].CGColor,(id)[UIColor orangeColor].CGColor];
    
    //将动画放入 动画组
    group.animations = @[animation1,animation2];
    //设置动画组 时间
    group.duration = 5;
    
    //添加动画
    [self.sunView.layer addAnimation:group forKey:nil];
    
}

- (IBAction)CATrasition:(id)sender {
    
    //过渡动画
    CATransition *transition = [CATransition animation];

    //配置动画
    transition.type = @"suckEffect";
    transition.subtype = @"fromLeft";
    
    transition.duration = 5;
    transition.repeatCount = 10;
    
    //添加动画
    [self.sunView.layer addAnimation:transition forKey:nil];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值