首页 > 技术文章 > CALayer的动画跟一个播放音乐的demo

4Dream 2015-06-25 16:06 原文

自动布局

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *buttonImg;

 

@end

 

@implementation ViewController

{

    AVAudioPlayer *audioPlayer;

}

- (void)viewDidLoad {

    [super viewDidLoad];

       // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma - buttonImg -

- (IBAction)buttonImg:(UIButton *)sender

{

    NSLog(@"%@",sender.currentTitle);

    NSURL *mp3URL = [[NSBundle mainBundle]URLForResource:@"111" withExtension:@"mp3"];

    NSLog(@"%ld",[sender.currentTitle compare:@"12"]);

    if([sender.currentTitle compare:@"12"])

    {

        mp3URL =  [[NSBundle mainBundle]URLForResource:@"画情透骨" withExtension:@"mp3"];

           }

    if(![sender.currentTitle compare:@"12"])

    {

        

        mp3URL =  [[NSBundle mainBundle]URLForResource:@"111" withExtension:@"mp3"];

    }

    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:mp3URL error:nil];

    audioPlayer.delegate = self;

    [audioPlayer play];

    [self scale:sender];

    }

#pragma - scale -

- (void) scale:(UIButton *)sender

{

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

    anim.values = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:sender.layer.transform],

                   [NSValue valueWithCATransform3D:CATransform3DScale(sender.layer.transform, 0.82, 0.82, 1)],

                   [NSValue valueWithCATransform3D:CATransform3DScale(sender.layer.transform, 1.5, 1.5, 1)], [NSValue valueWithCATransform3D:sender.layer.transform],nil];

    anim.duration = 0.5;

    anim.removedOnCompletion = YES;

    [sender.layer addAnimation:anim forKey:nil];

 

}

#pragma - move -

- (void) move:(UIButton *)sender

{

    CGPoint fromPoint = sender.layer.position;

    CGPoint toPoint  = CGPointMake(sender.frame.size.width+20, sender.frame.size.height);;

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];

    anim.fromValue = [NSValue valueWithCGPoint:fromPoint];

    anim.toValue = [NSValue valueWithCGPoint:toPoint];

    anim.duration = 0.3;

    sender.layer.position = toPoint;

    anim.removedOnCompletion = YES;

    [sender.layer addAnimation:anim forKey:nil];

}

#pragma - rotate -

- (void) rotate:(UIButton *)sender

{

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform "];

    CATransform3D fromValue = sender.layer.transform;

    anim.fromValue = [NSValue valueWithCATransform3D:fromValue];

    CATransform3D toValue = CATransform3DRotate (fromValue, M_PI, 1, 0,0);

//    CATransform3D toValue = CATransform3DRotate (fromValue, M_PI, 1, 0,0);

//x,y,z轴旋转180

    anim.toValue = [NSValue valueWithCATransform3D:toValue];

    anim.duration = 0.8;

    sender.layer.transform = toValue;

    anim.removedOnCompletion = YES;

    [sender.layer addAnimation:anim forKey:nil];

}

- (void) group:(UIButton *)sender

{

    CGPoint fromPoint = sender.layer.position;

    CGPoint toPoint = CGPointMake(sender.layer.position.x, sender.layer.position.y + 20);

    CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"position"]    ;

    moveAnim.fromValue = [NSValue valueWithCGPoint:fromPoint];

    moveAnim.toValue = [NSValue valueWithCGPoint:toPoint];

    CABasicAnimation *transformAnim = [CABasicAnimation animationWithKeyPath:@"transform"];

    CATransform3D fromValue = sender.layer.transform;

    transformAnim.fromValue = [NSValue valueWithCATransform3D:fromValue];

    CATransform3D scaleVlaue = CATransform3DScale(fromValue, 0.8, 0,8);

    CATransform3D rotateValue = CATransform3DRotate(fromValue, M_PI, 0, 0, 1);

    //计算两个变换矩阵的和

    CATransform3D toValue = CATransform3DConcat(scaleVlaue, rotateValue);

    //这是动画技术的属性

    transformAnim.toValue = [NSValue valueWithCATransform3D:toValue];

    transformAnim.cumulative = YES;//动画累加

    transformAnim.repeatCount = 2;///重复两次

    transformAnim.duration = 1;

    //3个一起执行

    CAAnimationGroup *animGroup = [CAAnimationGroup animation];

    animGroup.animations = [NSArray arrayWithObjects:moveAnim,transformAnim,nil];

    animGroup.duration = 2;

    [sender.layer addAnimation:animGroup forKey:nil];

}

@end

4个动画来自疯狂ios讲义里的  动画部分

推荐阅读