简体   繁体   中英

Attach a pan gesture recognizer an image

Could someone guide in what I need to do? I need to attach a pan gesture recognizer to an image which rotate clockwise and anticlockwise as we move.

Thanks

You'll want to attach a UIPanGestureRecognizer to the image and when the action method is called you can ask for the current translation and velocity of the gesture. You'd then rotate the image according to these values. Since this is a continuous gesture you'll be notified when the pan gesture changes, allowing you to update the rotation of the image.

UIPanGestureRecognizer Class Reference

--

Update

I've reread your question and if you want to rotate the image with your fingers by doing the typical rotate gesture, you should use the UIRotationGestureRecognizer instead of the UIPanGestureRecognizer . If you want to do a pan gesture moving your fingers left or right, you'll indeed have to use a UIPanGestureRecognizer . I just wanted to complete the answer.

Adding the rotation gesture recognizer:

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.myView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];

Handling the gesture and rotating the image accordingly could look like this (in its simplest form):

- (void)handleGesture:(UIRotationGestureRecognizer *)sender {
    self.myView.transform = CGAffineTransformMakeRotation(sender.rotation);
}

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