0

I am developing one application, in which i want a UIImage should rotate in a UIImageview 360 degree, in 3 dimensions. I have tried many codes but all the code with CAAnimation rotates whole image clockwise or flip the whole image view. So, how can i develop this type of functionality?

Pritesh
  • 980
  • 7
  • 17
  • Check this http://stackoverflow.com/questions/6531332/how-to-rotate-an-uiimageview-with-catransform3drotate-make-an-effect-like-door-o – kb920 Aug 06 '16 at 05:42

1 Answers1

2

In Swift, you can use the following code for infinite rotation: Swift -

let kRotationAnimationKey = "com.myapplication.rotationanimationkey" // Any key

func rotateView(view: UIView, duration: Double = 1) {
    if view.layer.animationForKey(kRotationAnimationKey) == nil {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = Float(M_PI * 2.0)
        rotationAnimation.duration = duration
        rotationAnimation.repeatCount = Float.infinity

        view.layer.addAnimation(rotationAnimation, forKey: kRotationAnimationKey)
    }
}

Stopping is like:

func stopRotatingView(view: UIView) {
    if view.layer.animationForKey(kRotationAnimationKey) != nil {
        view.layer.removeAnimationForKey(kRotationAnimationKey)
    }
}

Objective C

this worked perfectly for me: iphone UIImageView rotation

#import <QuartzCore/QuartzCore.h>

    - (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: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
        rotationAnimation.duration = duration;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = repeat;

        [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63