0

First post here, great site!

Basically, Im trying to rotate a UIImageView based on two points. Imagine the two points form a line going across the screen. I need the angle of the UIImageView to match the angle of that line...

Heres a diagram of what Im trying to say as its quite hard with words:

http://i40.tinypic.com/t5k2yr.png

Thanks for your time!

  • These might help: [link 1](http://stackoverflow.com/questions/8108768/ios-pinch-scale-and-two-finger-rotate-at-same-time), [link 2](http://iphonedevelopertips.com/event-handling/gestures-recognizers-tap-pinchzoom-rotate-swipe-pan-long-press.html) – tipycalFlow Apr 10 '12 at 16:09
  • Hmm those links seem to cover gesture recognisers, I was hoping to do the auto-rotation without any user input. – Tillmania Apr 10 '12 at 16:14
  • Use an `NSTimer` to change the angle of rotations and use the angle with transforms to make the rotations. – tipycalFlow Apr 10 '12 at 16:37
  • Im sorry but I dont fully understand what you mean. I believe that I first need to find the angle of the line generated by the two points, and then rotate the uiimageview to match that angle. Im not sure how those links help? – Tillmania Apr 10 '12 at 16:42
  • Those links were when I thought you need to rotate based on multiple-touch. Go through [this one](http://stackoverflow.com/questions/917713/uiimage-rotation-custom-degrees) instead – tipycalFlow Apr 10 '12 at 16:52
  • Finding the angle of the line is easy enough: atan((y2-y1)/(x2-x1)) for x2!=x1. If x2=x1, your angle is 90 degrees. – Ethan Brown Apr 10 '12 at 18:38
  • Thanks for the response. I get the atan() part but I'm not too sure about the "for" part. Would you mind explaining it a little for me? – Tillmania Apr 10 '12 at 22:20

1 Answers1

1

Would this work?

imageView.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));
JCooper
  • 6,395
  • 1
  • 25
  • 31