Animate Rotating UIImageView

前端 未结 5 1637
予麋鹿
予麋鹿 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:25

    A modern Swift solution, using NSTimer and CGAffineTransformMakeRotation:

    class Rotation: UIViewController {
        var timer = NSTimer()
        var rotAngle: CGFloat = 0.0
    
        @IBOutlet weak var rotationImage: UIImageView!
    
        override func viewDidLoad() {
          super.viewDidLoad()
          activateTimer()
        }
    
        func activateTimer() {
          //(1)
          timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target:self, selector:#selector(Rotation.updateCounter), userInfo: nil, repeats: true) 
        }
    
        func updateCounter() {
          //(2)
          var rotateLeft: Bool = true
          if rotateLeft {
            rotAngle -= 30.0
          } else {
            rotAngle += 30.0
          }
          //(3)
          UIView.animateWithDuration(2.0, animations: {
            self.rotationImage.transform = CGAffineTransformMakeRotation((self.rotAngle * CGFloat(M_PI)) / 180.0)
          })
        }
    }
    

    Things to notice:

    • Connect the outlet the the UIImageView

    • Play with the (1)timer pace, (2)animation pace and (3)rotation angle to get the desired results. This set of variables worked for me.

    0 讨论(0)
  • 2021-02-08 03:29

    Converted for Swift 3/4:

    let animation = CABasicAnimation(keyPath: "transform.rotation")
    animation.fromValue = 0
    animation.toValue =  Double.pi * 2.0
    animation.duration = 2
    animation.repeatCount = .infinity
    animation.isRemovedOnCompletion = false
    
    imageView.layer.add(animation, forKey: "spin")
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-08 03:42
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25]; // Set how long your animation goes for
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    player.transform = CGAffineTransformMakeRotation(angle); // if angle is in radians
    
    // if you want to use degrees instead of radians add the following above your @implementation
    // #define degreesToRadians(x)(x * M_PI / 180)
    // and change the above code to: player.transform = CGAffineTransformMakeRotation(degreesToRadians(angle));
    
    [UIView commitAnimations];
    
    // The rotation code above will rotate your object to the angle and not rotate beyond that.
    // If you want to rotate the object again but continue from the current angle, use this instead:
    // player.transform = CGAffineTransformRotate(player.transform, degreesToRadians(angle));
    
    0 讨论(0)
  • 2021-02-08 03:45

    I use some code like this to achieve a similar effect (though I rotate it by 360 degrees):

    CABasicAnimation *rotate;
    rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.fromValue = [NSNumber numberWithFloat:0];
    rotate.toValue = [NSNumber numberWithFloat:deg2rad(10)];
    rotate.duration = 0.25;
    rotate.repeatCount = 1;
    [self.view.layer addAnimation:rotate forKey:@"10"];
    

    I use code very similar to this to spin an image view.

    0 讨论(0)
提交回复
热议问题