I have a small issue with my gesture recognizers.
I have a class called "Sprite" which is just a UIImageView. Sprite has its own gesture recognizers and handling methods so that a user can pan, rotate, and resize the graphic.
Here is my code:
-(void)setup{ //sets up the imageview...
//add the image, frame, etc.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
[self addGestureRecognizer:panGesture];
[self addGestureRecognizer:pinchGesture];
[self addGestureRecognizer:rotateGesture];
}
//handling methods
-(void)handlePinch:(UIPinchGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
-(void)handleRotate:(UIRotationGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self]
}
So basically each of them works fine on their own. However, when I rotate or resize the imageView, panning becomes problematic. For example, if you rotate the imageView upside down, then the panning gestures will move the image in the reverse direction (up is down, dragging left moves it to the right, etc.). Similarly, a resized sprite will not pan at the same speed/distance as before.
Any ideas on how I can fix this? I would prefer to keep this code within the Sprite class rather than the ViewController (if possible). Thank you.
The user must press one or more fingers on a view while panning on the screen. A panning gesture is on continuous action when the user moves one or more fingers allowed (minimumNumberOfTouches) to enough distance for recognition as a pan.
Overview. A rotation gesture is a continuous gesture that occurs when the first two fingers that touch the screen rotate around each other.
A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.
Instead of translationInView:self, try translationInView:self.superview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With