2015-06-20 114 views
0

我试图让图像视图在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) 
} 
+0

第一:值应该是弧度,这意味着0 - 2PI而不是0 - 360秒:您可能需要使用CABasicAnimation和指定它是一种添加剂动画。 – luk2302

+0

@ luk2302 - 我不知道该怎么做(?) –

+0

http://stackoverflow.com/questions/17426802/using-cabasicanimation-to-rotate-a-uiimageview-more-than-once – luk2302

回答

2

可以使用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") 
+0

好的!谢谢。一个简单的问题:如何检测imageView是否停止旋转,而不使用定时器? –

+0

http://stackoverflow.com/a/297045/2303865 –

+0

我从来没有使用Objective-C。我只使用了Swift,所以我不知道如何使用Swift代码。 –