旋转图像查看随机

Rotate imageView random

我试图让图像视图在 360 到 720 度之间随机旋转。所以我希望图像视图旋转一次(360 度),然后再次随机旋转一次(0 到 360 度)

func rotateRandom2(){
    let lower : UInt32 = 360
    let upper : UInt32 = 720
    let diceRoll = CGFloat(arc4random_uniform(upper - lower) + lower)
    let degree =  0.0174532925 as CGFloat
    let rotate = diceRoll
    UIView.animateWithDuration(1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut,      animations: { () -> Void in
        self.bottleImageView.transform = CGAffineTransformRotate(self.bottleImageView.transform, rotate)
        }, completion: nil)
}

您可以使用 CABasicAnimation 为 z 旋转设置动画。您可以按照以下步骤操作:

let rotateView = CABasicAnimation()
let randonAngle = arc4random_uniform(361) + 360
rotateView.fromValue = 0
rotateView.toValue = Float(randonAngle) * Float(M_PI) / 180.0
rotateView.duration = 1
rotateView.repeatCount = 0
rotateView.removedOnCompletion = false
rotateView.fillMode = kCAFillModeForwards
rotateView.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
view.layer.addAnimation(rotateView, forKey: "transform.rotation.z")