3

How can I rotate a ImageView a random degree from 0 to 360?

This code let´s me rotate it 180 degrees:

func rotateTimes(){
        UIView.animateWithDuration(5, delay: 0.0, options: UIViewAnimationOptions.CurveLinear,      animations: { () -> Void in
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 180 * 0.0174532925)
            }, completion: nil)
    }

But I want to rotate random degree, not only 180 degrees.

I have tried:

func rotateTimes(){
        let diceRoll = Int(arc4random_uniform(7))
        UIView.animateWithDuration(5, delay: 0.0, options: UIViewAnimationOptions.CurveLinear,      animations: { () -> Void in
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, self.diceRoll * 0.0174532925)
            }, completion: nil)
    }

But that doesn´t work..

Torre Yan
  • 223
  • 3
  • 9

2 Answers2

0

Try this:

let random = arc4random_uniform(1000)
let angle = Double(2*pi*1000)/ Double(random)
UIView.animateWithDuration(5, 
  delay: 0.0, 
  options: UIViewAnimationOptions.CurveLinear,      
  animations: 
  { () -> Void in
    self.imageView.transform = 
      CGAffineTransformRotate(angle)
  }, 
  completion: nil)

(You'll need to define pi. If I remember correctly you have to import Darwin, and then the M_PI constant is available.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Add the line `import Darwin` at the top of your file, and then M_PI will be a defined constant that holds the value of pi. – Duncan C Apr 24 '15 at 17:42
  • You can also add an explicit `let pi = 3.1415926536`. That's plenty accurate enough for screen display. – Duncan C Apr 24 '15 at 17:43
0

You are rotating between 0 and 6 degrees. That is barely noticeable. Try multiples of 60 instead.

0.0174532925 represents a single degree on a circle of 360 degrees. Remember way back to algebra and circumferences of circles were 2*pi*r. Since we don't care about the length of the circumference we can drop r. The rotation around the circle is 2*pi. 2 * pi / 360 gives you the number above.

So to rotate the view, you decide how many degrees to rotate and multiply by 0.0174532925

I added a factor of 60 below to make the rotation more visible. 0 won't change the position. 1 will rotate it 1/6th of the way around the circle, 2 will rotate it 2/6th of the way around and so on. (6 will not change the position either.)

Also, it will rotate from the current position, not the starting position. So calling the function twice in a row and assuming a diceRoll of 3 both times, the view will end up back in the original position.

func rotateTimes(){
    let diceRoll = CGFloat(arc4random_uniform(7))
    let degree =  0.0174532925 as CGFloat
    let sixthOfCircle : CGFloat = 60
    let rotate = diceRoll * degree * sixthOfCircle
    UIView.animateWithDuration(5, delay: 0.0, options: UIViewAnimationOptions.CurveLinear,      animations: { () -> Void in
        self.rotateView.transform = CGAffineTransformRotate(self.rotateView.transform, rotate)
        }, completion: nil)
}
HughV
  • 86
  • 6