图像在iOS中以3D形式在图像视图中旋转360度

时间:2016-08-06 05:37:25

标签: ios image-rotation caanimation

我正在开发一个应用程序,我想要一个UIImage应该在UIImageview 360度旋转,3维。 我尝试了很多代码,但所有带CAAnimation的代码都顺时针旋转整个图像或翻转整个图像视图。 那么,我该如何开发这种功能呢?

1 个答案:

答案 0 :(得分:2)

在Swift中,您可以使用以下代码进行无限循环: 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)
    }
}

停止就像:

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

目标C

这对我来说很完美:iphone UIImageView旋转

#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"];
    }