简体   繁体   中英

How to rotate a UIImageView inside a UIScrollView using UIGestureRecognizers, relative to the touch location

I am looking for an example of how to implement UIRotationGestureRecognizer of a UIImageView inside a UIScrollView, where the rotation is performed relative to where the touches occur, rather than relative to the origin of the UIImageView.

In most examples I've found, the ImageView is rotated around its own center point. In other words, if you place 2 fingers in the top left corner of the image, and twist them (to perform the rotation gesture), the UIImageView will rotate around its own origin.

The behaviour I'm looking for would have the UIImageView pivot around the touch points.

While there are plenty of tutorials on how to implement UIRotationGestureRecognizer for an ImageView inside a ScrollView, I haven't found any that describe how to perform a rotation that is relative to the touches.

A UIRotationGestureRecognizer returns an angle of rotation.

The base class, UIGestureRecognizer , has a method

location(ofTouch:in:) and a var numberOfTouches

In your valueChanged handler for your gesture recognizer you should be able to fetch the 2 touches, find the midpoint of those two points, and apply your rotation around that rather than the center of the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM