ylliX - Online Advertising Network

Rotating and moving UIImageView

Just simple conclusion – NEVER EVER try to move UIImageView which you are rotating using CATransform3DMakeRotation or CGAffineTransformMakeRotation. Even combining CGAffineTransformRotate with CGAffineTransformMakeTranslation together in CGAffineTransformConcat function gives unpredictable movement. So what to do? The best solution is to not move image. But if for some reasons you have to – put it in UIView and move UIView while rotating UIImageView. But bewere – there is ANOTHER TRAP waiting for you. DO NOT move UIView by changing its frame property, it will also cause unpredictable movement. You have to use CGAffineTransformTranslate and move it using delta values. So here is your workflow:

1. get movement delta (you can just get current UIView frame and substract it from new desired location)
2. rotate UIImageView using myimage.layer.transform = CATransform3DMakeRotation(degrees / 180.0 * M_PI, 0, 0, 1) – since rotation is in radians you have to divide it by 180.0 * M_PI
3. finally move your UIView using myview.transform = CGAffineTransformTranslate(myview.transform, 0, deltaY) where deltaY is value you have from step 1

Leave a Reply