iOS-核心動(dòng)畫

coreAnimation.png
  1. 基本動(dòng)畫CABasicAnimation,確定keyPath,animation的duration、fromValue以及toValue。
 // KeyPath確定為position.x,
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];   
  //動(dòng)畫執(zhí)行的duration
    animation.duration = 0.8f;
 //動(dòng)畫執(zhí)行的fromValue到tovalue
    animation.fromValue = @(self.redView.center.x);
    animation.toValue = @(self.redView.center.x+50);
    
    //一個(gè)時(shí)間函數(shù),表示它是以怎么樣的時(shí)間運(yùn)行
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    animation.repeatCount = HUGE_VALF;
    animation.repeatDuration = 2;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [self.redView.layer addAnimation:animation forKey:nil];
  1. 關(guān)鍵幀動(dòng)畫,動(dòng)畫可以按照一個(gè)指定的path執(zhí)行,或者按照一組特定的values去執(zhí)行。
//關(guān)鍵幀動(dòng)畫
-(void)keyFrameAnimation{
//動(dòng)畫執(zhí)行的路徑
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.redView.center radius:100 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    CAKeyframeAnimation *keyFrameAnmiation = [CAKeyframeAnimation animation];
    keyFrameAnmiation.path = path.CGPath;
    //動(dòng)畫執(zhí)行的keyPath
    keyFrameAnmiation.keyPath = @"position";
    keyFrameAnmiation.duration = 5;
    keyFrameAnmiation.repeatCount = MAXFLOAT;
    [self.redView.layer addAnimation:keyFrameAnmiation forKey:nil];
}
  1. 動(dòng)畫組,
//動(dòng)畫組
-(void)animationGroup{
    //1.創(chuàng)建一個(gè)基礎(chǔ)動(dòng)畫
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    basicAnimation.keyPath = @"transform.scale";
    basicAnimation.fromValue = @1.0;
    basicAnimation.toValue = @0.2;
    
    //2.創(chuàng)建一個(gè)幀動(dòng)畫
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
    keyFrameAnimation.keyPath = @"position";
    keyFrameAnimation.path = [UIBezierPath bezierPathWithArcCenter:self.redView.center radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES].CGPath;

    //3.創(chuàng)建一個(gè)動(dòng)畫組
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 5.0f;
    group.autoreverses = YES;
    group.animations = @[basicAnimation,keyFrameAnimation];
    
    [self.redView.layer addAnimation:group forKey:nil];
}
  1. 轉(zhuǎn)場(chǎng)動(dòng)畫


    May-14-2020 18-52-28.gif
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,200, 200)];
    self.imgView.backgroundColor = UIColor.redColor;
    self.imgView.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:self.imgView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    static int index = 1;
    index++;
    NSString *imgName = [NSString stringWithFormat:@"%d.jpg",index];
    self.imgView.image = [UIImage imageNamed:imgName];
    if (index == 4) {
        index = 1;
    }
    
    CATransition *transition = [CATransition animation];
    transition.type = @"pageCurl";
    transition.subtype = kCATransitionFromLeft;
    transition.duration = 1.0f;
    [self.imgView.layer addAnimation:transition forKey:nil];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容