CoreAnimation的基本用法

@interface ViewController ()
{
    
    CustomButton* but;
    int currentCount;

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    but = [[CustomButton alloc]initWithFrame:CGRectMake(0,0, 64, 64)];//接上一篇重绘的按钮

    [self.view addSubview:but];
    [but addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
	// Do any additional setup after loading the view, typically from a nib.
}
-(void)click{

    switch (currentCount%4) {
        case 0:
        {
            CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position"];
            anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
            anim.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 288)];
            anim.duration = 2;
            but.layer.position = CGPointMake(160, 288);
            anim.removedOnCompletion = YES;
            [but.layer addAnimation:anim forKey:nil];
        }
            break;
        case 1:{
            
            CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform"];
            CATransform3D fromValue = but.layer.transform;
            // 设置动画开始的属性值
            anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
            // 绕X轴旋转180度
            CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 1 , 0 , 0);
            // 绕Y轴旋转180度
            //	CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 1 , 0);
            //	// 绕Z轴旋转180度
            //	CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
            // 设置动画结束的属性值
            anim.toValue = [NSValue valueWithCATransform3D:toValue];
            anim.duration = 0.5;
            anim.repeatCount = 13;
            but.layer.transform = toValue;
            anim.removedOnCompletion = YES;
            // 为imageLayer添加动画
            [but.layer addAnimation:anim forKey:nil];
        }break;
        case 2:{
            CAKeyframeAnimation* anim = [CAKeyframeAnimation
                                         animationWithKeyPath:@"transform"];
            // 设置CAKeyframeAnimation控制transform属性依次经过的属性值
            anim.values = [NSArray arrayWithObjects:
                           [NSValue valueWithCATransform3D:but.layer.transform],
                           [NSValue valueWithCATransform3D:CATransform3DScale
                            (but.layer.transform , 0.2, 0.2, 1)],
                           [NSValue valueWithCATransform3D:CATransform3DScale
                            (but.layer.transform, 2, 2 , 1)],
                           [NSValue valueWithCATransform3D:but.layer.transform], nil];
            anim.duration = 5;
            anim.removedOnCompletion = YES;
            // 为imageLayer添加动画
            [but.layer addAnimation:anim forKey:nil];
            
        }
            break;
        case 3:{
            
            CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform"];
            CATransform3D fromTransValue = but.layer.transform;
            // 设置动画开始的属性值
            anim.fromValue = [NSValue valueWithCATransform3D:fromTransValue];
            // 绕X轴旋转180度
            CATransform3D toTransValue = CATransform3DRotate(fromTransValue, M_PI , 1 , 0 , 0);
            // 绕Y轴旋转180度
            //	CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 1 , 0);
            //	// 绕Z轴旋转180度
            //	CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
            // 设置动画结束的属性值
            anim.toValue = [NSValue valueWithCATransform3D:toTransValue];
            anim.duration = 0.5;
            anim.repeatCount = 13;
            but.layer.transform = toTransValue;
            anim.removedOnCompletion = YES;
            // 为imageLayer添加动画
            [but.layer addAnimation:anim forKey:nil];
            anim.removedOnCompletion = YES;
            // 创建不断改变CALayer的transform属性的属性动画
            CABasicAnimation* transformAnim = [CABasicAnimation
                                               animationWithKeyPath:@"transform"];
            CATransform3D fromValue = but.layer.transform;
            // 设置动画开始的属性值
            transformAnim.fromValue = [NSValue valueWithCATransform3D: fromValue];
            // 创建缩放为X、Y两个方向上缩放为0.5的变换矩阵
            CATransform3D scaleValue = CATransform3DScale(fromValue , 0.5 , 0.5, 1);
            // 绕Z轴旋转180度的变换矩阵
            CATransform3D rotateValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
            // 计算两个变换矩阵的和
            CATransform3D toValue = CATransform3DConcat(scaleValue, rotateValue);
            // 设置动画技术的属性值
            transformAnim.toValue = [NSValue valueWithCATransform3D:toValue];
            // 动画效果累加
            transformAnim.cumulative = YES;
            // 动画重复执行2次,旋转360度
            transformAnim.repeatCount = 2;
            transformAnim.duration = 3;
            // 位移、缩放、旋转组合起来执行
            CAAnimationGroup *animGroup = [CAAnimationGroup animation];
            animGroup.animations = [NSArray arrayWithObjects:anim
                                    , transformAnim , nil];
            animGroup.duration = 6;
            // 为imageLayer添加动画
            [but.layer addAnimation:animGroup forKey:nil];
            
            
        }
        default:
            break;
    }
    currentCount++;


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值