iOS 核心動畫CAAnimation的子類的介紹及使用

基本動畫(CABaseAnimation)
動畫

#import "ViewController.h"

@interface ViewController ()
{
    UIImageView *_imgView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    _imgView= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _imgView.backgroundColor = [UIColor lightGrayColor];
    _imgView.image = [UIImage imageNamed:@"Appicon@3x.png"];
    [self.view addSubview:_imgView];
  
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 獲取動畫對象
    CABasicAnimation *animation = (CABasicAnimation *)[_imgView.layer animationForKey:@"rotationAnimation"];
    if (animation == nil)
    {
        //旋轉
        [self rotationAnimation];
    }else if(_imgView.layer.speed == 1)
    {
        //暫停
        [self pauseAnimation];
    }else if (_imgView.layer.speed == 0)
    {
        //開始
        [self startAnimation];
    }

}

- (void)rotationAnimation
{
    //建立基本動畫
    CABasicAnimation *imganimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    imganimation.duration = 3;//設置時間
    imganimation.repeatCount = MAXFLOAT;//設置重複次數
    imganimation.fromValue = 0;//設置開始值
    imganimation.toValue = @(M_PI*2);//設置結束值
    [_imgView.layer addAnimation:imganimation forKey:@"rotationAnimation"];//將動畫添加到圖層上去

}
- (void)pauseAnimation
{
    //獲取當前的便宜時間
    _imgView.layer.timeOffset = [_imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    _imgView.layer.speed = 0;
    
    
}

- (void)startAnimation
{

    _imgView.layer.beginTime = CACurrentMediaTime() - (_imgView.layer.timeOffset);
    _imgView.layer.timeOffset = 0;
    _imgView.layer.speed = 1;
}


- (void)scaleAnimation
{
    //獲取動畫對象
    CABasicAnimation *imgAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    
    imgAnimation.duration = 1;
    //設置初始值
    imgAnimation.fromValue = @1;
    //設置結束值
    imgAnimation.toValue = @2;
    
    //使動畫結束在以動畫的形式恢復
    imgAnimation.autoreverses = YES;
    
    //動畫結束後停留在結束的狀態
    imgAnimation.removedOnCompletion = NO;
    
    imgAnimation.fillMode = @"forwards";
    
    [_imgView.layer addAnimation:imgAnimation forKey:@"animation"];
    

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

關鍵幀動畫(CAKeyframeAnimation)spa

//讓動畫圍繞觸摸的地方畫圓
- (void)movieWithArc:(CGPoint)touchPoint {

    //以手指點擊的地方做爲圓心,150做爲半徑的圓運動
    
    //建立動畫對象
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //設置屬性
    keyFrameAnimation.duration = 2;
    
    keyFrameAnimation.repeatCount = MAXFLOAT;
    
    //設置運動的路徑
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddArc(path, NULL, touchPoint.x, touchPoint.y, 150, 0, M_PI*2, 1);
    
    keyFrameAnimation.path = path;
    //釋放路徑
    CGPathRelease(path);
    
    //添加動畫
    [_imgView.layer addAnimation:keyFrameAnimation forKey:nil];
}

動畫組:CAAnimationGroupcode

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];
    
    //建立動畫組對象
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    //設置屬性
    CAAnimation *animation1 = [self movieWithArc:p];
    CAAnimation *animation2 = [self getAnimation];
    
    group.animations = @[animation1,animation2];
//    group.repeatCount = MAXFLOAT;
    group.duration = 3;
    
    group.delegate = self;
    
    [_imgView.layer addAnimation:group forKey:nil];
    
}

//圓周運動
- (CAKeyframeAnimation *)movieWithArc:(CGPoint)touchPoint {
    
    //以手指點擊的地方做爲圓心,150做爲半徑的圓運動
    
    //建立動畫對象
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //設置屬性
    keyFrameAnimation.duration = 2;
    
    keyFrameAnimation.repeatCount = MAXFLOAT;
    
    //設置運動的路徑
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddArc(path, NULL, touchPoint.x, touchPoint.y, 150, 0, M_PI*2, 1);
    
    keyFrameAnimation.path = path;
    //釋放路徑
    CGPathRelease(path);
    
    return keyFrameAnimation;
}

//搖晃
- (CAKeyframeAnimation *)getAnimation {

    //建立動畫化對象
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    
    //設置屬性
    CGFloat num1 = M_PI_4/5.0;
    CGFloat num2 = -M_PI_4/5.0;
    
    animation.values = @[@(num1),@(num2),@(num1)];
    
    animation.duration = 0.5;
    animation.repeatCount = MAXFLOAT;
    
    //加速方式
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    return animation;
    
}

CATransition轉場動畫orm

    //建立動畫對象對象

    CATransition *transition = [[CATransition alloc] init];ip

    

    //設置屬性rem

    transition.duration = 1;get

    

    //設置動畫類型animation

    

     提供了:it

  1.      kCATransitionFade  漸變

  2.      kCATransitionMoveIn 進入

  3.      kCATransitionPush  推入

  4.      kCATransitionReveal  移除

     

 5. rippleEffect  水滴效果  cameraIrisHollowClose相機關閉的效果

    transition.type = @"cameraIrisHollowOpen";


    //設置動畫子類型

    transition.subtype = kCATransitionFromRight;

    //添加

    [_imgView.layer addAnimation:transition forKey:nil];

相關文章
相關標籤/搜索