iOS 旋转动画

旋转动画有三种方法:

1.gif
  • UIView动画
-(void) startAnimation{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(endAnimation)];
    self.innercircleImageView.transform = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
    self.circleImageView.transform = CGAffineTransformMakeRotation(self.angle * (M_PI / -180.0f));
    [UIView commitAnimations];
}
-(void)endAnimation
{
    self.angle += 10;
    [self startAnimation];
}
  • UIView动画Block实现
-(void)startAnimation0
{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(self.angle * (M_PI / -180.0f));
     CGAffineTransform endAngle0 = CGAffineTransformMakeRotation(self.angle * (M_PI / 180.0f));
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.circleImageView.transform = endAngle;
        self.innercircleImageView.transform = endAngle0;
    } completion:^(BOOL finished) {
        self.angle += 10;
        [self startAnimation0];
    }];
}
  • CABasicAnimation动画
-(void)startAnimation1{
    self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    self.rotationAnimation.duration = 1.5;
    self.rotationAnimation.cumulative = YES;
    self.rotationAnimation.repeatCount = MAXFLOAT;
    [self.innercircleImageView.layer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];
    
    self.rotationAnimation0 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotationAnimation0.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 ];
    self.rotationAnimation0.duration = 3;
    self.rotationAnimation0.cumulative = YES;
    self.rotationAnimation0.repeatCount = MAXFLOAT;
    [self.circleImageView.layer addAnimation:self.rotationAnimation0 forKey:@"rotationAnimation0"];
}
  • 使用CGPath绘制路线执行:用CoreGraphics库中的CGPathAddArc方法
-(void)startAnimation2
{
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddArc(path, NULL, self.innercircleImageView.center.x, self.innercircleImageView.center.y, 1, 0,M_PI * 2, 0);
    
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    animation.path = path;
    
    CGPathRelease(path);
    
    animation.duration = 3;
    
    animation.repeatCount = 500;
    
    animation.autoreverses = NO;
    
    animation.rotationMode =kCAAnimationRotateAuto;
    
    animation.fillMode =kCAFillModeForwards;
    
    [self.innercircleImageView.layer addAnimation:animation forKey:@"animation"];
    
    CGMutablePathRef path0 = CGPathCreateMutable();
    
    CGPathAddArc(path0, NULL, self.circleImageView.center.x, self.circleImageView.center.y, 1, 0,M_PI * 2, 1);
    
    CAKeyframeAnimation * animation0 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    animation0.path = path0;
    
    CGPathRelease(path0);
    
    animation0.duration = 3;
    
    animation0.repeatCount = 500;
    
    animation0.autoreverses = NO;
    
    animation0.rotationMode =kCAAnimationRotateAuto;
    
    animation0.fillMode =kCAFillModeForwards;
    
    [self.circleImageView.layer addAnimation:animation0 forKey:@"animation0"];

}

Demo地址。

你可能感兴趣的:(iOS 旋转动画)