Animate Rotating UIImageView

前端 未结 5 1674
予麋鹿
予麋鹿 2021-02-08 03:07

I want to rotate a UIImageView by roughly 10 degrees left/right but have a smooth animation, rather than a sudden turn which I see using:

play         


        
5条回答
  •  不知归路
    2021-02-08 03:31

    i have some code rotate 360 degrees:

      - (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat;
    {
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: rotations * 2.0 /* full rotation*/ * rotations * duration ];
        rotationAnimation.duration = duration;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = repeat;
        [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    

    call it: [self runSpinAnimationOnView:imgView duration:0.1 rotations:M_PI_4 repeat:10];

    And animation rotate some degrees and again:

        - (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
                  curve:(int)curve rotations:(CGFloat)rotations
    {
        [UIView animateWithDuration:duration
                              delay:delay
                            options:0
                         animations:^{
                             [UIView setAnimationCurve:curve];
                             image.transform = CGAffineTransformMakeRotation(rotations);
    
                         }
                         completion:^(BOOL finished){
                             [self rotateImage2:image duration:duration delay:delay curve:curve rotations:rotations];
                             ;
                         }];
        [UIView commitAnimations];
    
    }
    - (void)rotateImage2:(UIImageView *)image duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
                   curve:(int)curve rotations:(CGFloat)rotations
    {
        [UIView animateWithDuration:duration
                              delay:delay
                            options:0
                         animations:^{
                             [UIView setAnimationCurve:curve];
                             image.transform = CGAffineTransformMakeRotation(-rotations);
                         }
                         completion:^(BOOL finished){
                             [self rotateImage:image duration:duration delay:delay curve:curve rotations:rotations];
                             ;
                         }];
        [UIView commitAnimations];
    
    }
    

    If you want to rotate a uiimageview by roughly 10 degrees left/right call : [self rotateImage:imgView duration:0.7 delay:0.0 curve:UIViewAnimationCurveEaseIn rotations:(M_PI/18)]; similar, some degress

提交回复
热议问题