2025-05-23

第一種 隱式動畫

這是一種最簡單的動畫,不用設置定時器,不用考慮線程或者重畫

實現代碼:

#import

-(void)clickButton:(UIButton*)button
{
    [UIView beginAnimations:nil
                    context:nil];
    CGAffineTransform transform=CGAffineTransformMakeTranslation(180, 200);
    [self.imageView.layer setAffineTransform:transform];
    self.imageView.layer.opacity=1;
    [UIView commitAnimations];
}

操作layer,IOS的動畫都是操作layer

第二種、顯示動畫

 
    CABasicAnimation *opAnim=[CABasicAnimation animationWithKeyPath:@"opacity"];
    opAnim.duration=6.0;
    opAnim.fromValue=[NSNumber numberWithFloat:25];
    opAnim.toValue=[NSNumber numberWithFloat:1.0];
    opAnim.cumulative=YES;
    opAnim.repeatCount=1;
    [self.imageView.layer addAnimation:opAnim forKey:@"animateOpacity"];
    
    CGAffineTransform moveTransform=CGAffineTransformMakeTranslation(200, 200);
    CABasicAnimation *moveAnim=[CABasicAnimation animationWithKeyPath:@"transform"];
    moveAnim.duration=6.0;
    moveAnim.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(moveTransform)];
    [self.imageView.layer addAnimation:moveAnim forKey:@"animateTransform"];

第三種、關鍵幀顯示動畫

CAKeyframeAnimation *opAnim=[CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opAnim.duration=6.0;
    opAnim.values=@[@0.25,@0.75,@1.0];
    opAnim.keyTimes=@[@0.0,@0.5,@1.0];
    [self.imageView.layer addAnimation:opAnim forKey:@"animateOpacity"];
    
    CGAffineTransform moveTransform=CGAffineTransformMakeTranslation(180, 200);
    CABasicAnimation *moveAnim=[CABasicAnimation animationWithKeyPath:@"transform"];
    moveAnim.duration=6.0;
    moveAnim.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeAffineTransform(moveTransform)];
    moveAnim.delegate=self;
    [self.imageView.layer addAnimation:moveAnim forKey:@"animateTransform"];

values是一個值的數組

keyTimes是一個每個幀片段持續的時間比例,取值范圍是0.0-1.0之前

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *