简体   繁体   中英

Pan gesture messes up direction based on rotation

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.

而不是translationInView:self,尝试translateInView:self.superview。

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