0

I am trying to get the image view to rotate random degree between 360 and 720. So I want the image view to rotate once(360 degrees), and then again rotate a random degree(0 to 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)
}
rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • first: the values should be radians, meaning 0 - 2PI instead of 0 - 360. second: you might have to use a CABasicAnimation and specify that it is an additive animation. – luk2302 Jun 20 '15 at 21:29
  • @luk2302 - I have NO idea how to do that(?) – James Offerd Jun 20 '15 at 21:30
  • http://stackoverflow.com/questions/17426802/using-cabasicanimation-to-rotate-a-uiimageview-more-than-once – luk2302 Jun 20 '15 at 21:31
  • Shouldn't you be multiplying something by `degree`. `let rotate = diceRoll * degree` perhaps? – vacawama Jun 20 '15 at 21:32
  • What problems do you face with what you have tried? From other comments, it looks like you want to find our the difference between radians and degrees, and which is used for rotation. – Wai Ha Lee Jun 20 '15 at 21:47
  • Note that `degree` is 1 degree in radians, so `360 * degree` is 360 degrees in radians. – vacawama Jun 20 '15 at 21:51
  • http://stackoverflow.com/a/29179878/2303865 – Leo Dabus Jun 20 '15 at 21:58

1 Answers1

2

You can use CABasicAnimation to animate z rotation. You can do as follow:

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")
Leo Dabus
  • 198,248
  • 51
  • 423
  • 494
  • Good one! Thanks. A quick questions: How can i detect if the imageView has stopped rotating, without using a timer? – James Offerd Jun 20 '15 at 22:07
  • I have never used Objective-C. I have only used Swift, so i do not know how to use that code for Swift. – James Offerd Jun 20 '15 at 22:26
  • Feel free to open another question asking how to add a completion handler to a cabasic animation. Don't forget to select the checkmark below the votes counter if that answer answered your original question – Leo Dabus Jun 20 '15 at 22:28
  • You can also use a delay function to execute what you want after a specific time, in that case the time to complete the rotation – Leo Dabus Jun 20 '15 at 22:34
  • @JamesOfferd note: if you want 720 inclusive you need to use arc4random_uniform(361) + 360 – Leo Dabus Jun 21 '15 at 00:10