1

I have a horizontal scrollview with an image which I need to rotate as the user scrolls left or right. It needs to rotate as the user moves the scroll across the screen. So if the user scrolls half way and stops the image should rotate halfway etc. I have looked at few examples but nothing seems to be giving me the correct result. Can some one please help.

Thanks in advance

dogwasstar
  • 852
  • 3
  • 16
  • 31

3 Answers3

1

Vertical scrolling and Swift 5:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let topOffset = scrollView.contentOffset.y
    let angle = -topOffset * 2 * CGFloat(Double.pi / 180)
    self.myImageView.transform = CGAffineTransform(rotationAngle: angle)
}

Don't forget to do this in viewDidLoad:

self.scrollView.delegate = self
IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16
0

You'll need to implement the scrollViewDidScroll method and then apply a rotation matrix to the image. To rotate a UIIMage you can do what they outline here. How to Rotate a UIImage 90 degrees?

However, this would be better accomplished in a pan gesture recognizer. The pan gesture will give you information about how far the user panned, and then you can rotate the image based on that distance.

Community
  • 1
  • 1
Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
0

To rotate image is not good for performance. You'd better set image view's rotate transform as user scrolls. Like this:

imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
Lumialxk
  • 6,239
  • 6
  • 24
  • 47