iOS CoreAnimation

@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UILabel *label;

#pragma mark - CoreAnimation
//基础动画
- (IBAction)CABasicAnimation:(UIButton *)sender {
    //参数需要是layer的属性,或者是属性的属性的名字
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];
    basic.fromValue = @100;
    basic.toValue = @250;
    basic.duration = 3;
    basic.repeatCount = 1;
    //需要手动改变位置,(如果需要改变position的真实位置)
    //self.redView.layer.position = CGPointMake(150, 200);
    //key,标识
    [self.redView.layer addAnimation:basic forKey:nil];
}

//关键帧动画
- (IBAction)keyFrameAnimation:(UIButton *)sender {
    
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGPoint point0 = CGPointMake(100, 100);
    CGPoint point1 = CGPointMake(100, 200);
    CGPoint point2 = CGPointMake(200, 200);
    CGPoint point3 = CGPointMake(200, 300);
    CGPoint point4 = CGPointMake(150, 300);
    CGPoint point5 = CGPointMake(175, 250);
    CGPoint point6 = CGPointMake(175, 100);
    
    NSValue *value1 = [NSValue valueWithCGPoint:point0];
    NSValue *value2 = [NSValue valueWithCGPoint:point1];
    NSValue *value3 = [NSValue valueWithCGPoint:point2];
    NSValue *value4 = [NSValue valueWithCGPoint:point3];
    NSValue *value5 = [NSValue valueWithCGPoint:point4];
    NSValue *value6 = [NSValue valueWithCGPoint:point5];
    NSValue *value7 = [NSValue valueWithCGPoint:point6];
    
    //根据一组点来创建动画
    keyFrameAnimation.values = @[value1, value2, value3, value4, value5, value6, value7, value1];
    keyFrameAnimation.duration = 10;
    [self.redView.layer addAnimation:keyFrameAnimation forKey:nil];
}


/*
 *专场动画
 *pageCurl            向上翻一页
 *pageUnCurl          向下翻一页
 *rippleEffect        滴水效果
 *suckEffect          收缩效果,如一块布被抽走
 *cube                立方体效果
 *oglFlip             上下翻转效果
 */
- (IBAction)transitionAnimation:(UIButton *)sender {
    
    CATransition *transition = [CATransition animation];
    transition.type = @"rippleEffect";  //动画过渡类型
    transition.subtype = kCATransitionFromLeft; //动画过度方向
    transition.repeatCount = HUGE_VALL; //动画重复次数,最大次数
    transition.duration = 2;    //动画持续时间
    [self.redView.layer addAnimation:transition forKey:nil];
}


// 过度动画, 转场动画
- (IBAction)transitionAnimation1:(UIButton *)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    // 第一个参数: 转场动画的类型
    // 第二个参数: 给哪个对象添加转场动画
    // 第三个参数: 缓存设置(YES执行效率更高)
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    self.label.text = @"熊大熊二的故事";
    [UIView commitAnimations];
}




//CAAnimationGroup。组动画实际上就是可以将上面的几种动画类型随意组合添加到一个对象上。
- (IBAction)groupAnimation:(UIButton *)sender {
    //创建一个缩放动画
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basic.fromValue = @1;
    basic.toValue = @0.75;
    //创建一个关键帧动画,从一个位置到另一个位置
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGPoint point1 = CGPointMake(100, 100);
    CGPoint point2 = CGPointMake(300, 300);
    NSValue *value1 = [NSValue valueWithCGPoint:point1];
    NSValue *value2 = [NSValue valueWithCGPoint:point2];
    keyFrameAnimation.values = @[value1, value2];
    //创建一个动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 4;
    group.repeatCount = 3;
    //将上面的两种动画添加到动画组中,动画将同步执行
    [group setAnimations:@[basic,keyFrameAnimation]];
    //下面的代码会最终改变对象的位置,否则对象在动画完成后会回到初始位置
    self.redView.layer.position = CGPointMake(300, 300);
    //添加动画组
    [self.redView.layer addAnimation:group forKey:nil];
}



/*
 transform.scale = 比例轉換
 transform.scale.x = 闊的比例轉換
 transform.scale.y = 高的比例轉換
 transform.rotation.z = 平面圖的旋轉
 opacity = 透明度
 margin
 zPosition
 backgroundColor    背景颜色
 cornerRadius    圆角
 borderWidth
 bounds
 contents
 contentsRect
 cornerRadius
 frame
 hidden
 mask
 masksToBounds
 opacity
 position
 shadowColor
 shadowOffset
 shadowOpacity
 */
- (IBAction)springAnimation:(UIButton *)sender {
    
    CASpringAnimation *spring = [CASpringAnimation animationWithKeyPath:@"transform.rotation"];
    //质量越大,惯性越大
    spring.mass = 1;
    //阻尼系数,越大停止的越快
    spring.damping = 0.2;
    spring.fromValue = @0.2;//transform.rotation的初始值
    spring.toValue = @0.75;//transform.rotation的结束值
    spring.repeatCount = 1;
    spring.autoreverses = YES;//自动回弹
    //初始速度
    spring.initialVelocity = 5;
    spring.duration = spring.settlingDuration;
    [self.redView.layer addAnimation:spring forKey:@"spring"];
}



// 弹簧动画
- (IBAction)sprintAnimation:(UIButton *)sender {
    // 1. 动画时长
    // 2. 延迟时间
    // 3. 阻尼系数, 越大, 弹簧形变越小
    // 4. 速度
    // 5. 动画类型选项
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint point = self.redView.center;
        point.y += 260;
        self.redView.center = point;
        
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}


#pragma mark
#pragma mark - UIViewAnimation
- (IBAction)changeFrame:(UIButton *)sender {
    //第一个参数,动画标示
    [UIView beginAnimations:@"changeFrame" context:nil];
    //设置动画次数
    [UIView setAnimationRepeatCount:100];
    //设置动画时长
    [UIView setAnimationDuration:1];
    //设置动画速率
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    CGRect frame = self.redView.frame;
    frame.origin.y = 200;
    //改变frame,y值
    self.redView.frame = frame;
    //开始动画
    [UIView commitAnimations];
}

//缩放动画
- (IBAction)scaleButtonAction:(UIButton *)sender {
    
    [UIView beginAnimations:@"UIView" context:nil];
    self.redView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 1.5);
}


// 旋转
- (IBAction)rotateButtonAction:(UIButton *)sender {
    [UIView beginAnimations:nil context:nil];
    // 第一个参数: 以谁为基准进行旋转
    // 第二个参数: 旋转的弧度
    // CGAffineTransformIdentity 原始形变
    [UIView setAnimationDuration:1];
    self.redView.transform = CGAffineTransformRotate(self.redView.transform, -M_PI / 180 * 30);
    [UIView commitAnimations];
}


// 简单block动画
- (IBAction)simpleBlock:(UIButton *)sender{
    [UIView animateWithDuration:2 animations:^{
        CGPoint point = self.redView.center;
        point.y += 100;
        self.redView.center = point;
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
        [UIView animateWithDuration:2 animations:^{
            CGPoint point = self.redView.center;
            point.y -= 100;
            self.redView.center = point;
        } completion:nil];
        
    }];
}

// 复杂的block
- (IBAction)complexBlock:(UIButton *)sender {
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.redView.transform = CGAffineTransformRotate(self.redView.transform, M_PI / 180 * 90);
    } completion:^(BOOL finished) {
        // 还原到初始形变
        //        self.planeImageView.transform = CGAffineTransformIdentity;
    }];
}

 代码收集来自: http://www.cnblogs.com/zzuliliu/p/5468732.html

原文地址:https://www.cnblogs.com/mapanguan/p/5469512.html