简体   繁体   中英

Rotate UIView while dragging

Using a UIPanGestureRecognizer , I'm allowing the user to drag a UIView . What I now want to accomplish is getting the view to adjust its rotation around a specified point DURING dragging.

Examples of this can be found in the Tinder App (when dragging a portrait the images rotate slightly) or in the Path App (when dragging a friends popup up or down it rotates to the side grabbed on).

I must admit that I have very little experience working with UIPanGestureRecognizer , but I assume that it won't affect the answer.

Nevertheless one solution could be to create a timer and make it run while the UIView is being dragged, the timer action would then make sure to update the rotation of the UIView properly and as often as possible.

Another solution could be to subclass UIView and overwrite the drag-function (I do not know what it is called) and simply make it self adjust its rotation if any pivot point is given.

Do the rotation in the following method

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.yourView.transform = CGAffineTransformMakeRotation(CGFloat angle);
}

Use CATransform3D

Add

#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)

in .pch file

CATransform3D myTransform = CATransform3DIdentity;
myTransform.m34 = 1.0 / -500;
myTransform = CATransform3DRotate(myTransform, DEGREES_TO_RADIANS(90), 0.0f, 0.0f, 1.0f);
myView.layer.transform = myTransform;

you can go on changing the angle here DEGREES_TO_RADIANS(90) Hope this will help you

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