简体   繁体   English

平移姿势基于旋转弄乱了方向

[英]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”的类,它只是一个UIImageView。 Sprite has its own gesture recognizers and handling methods so that a user can pan, rotate, and resize the graphic. Sprite有自己的手势识别器和处理方法,因此用户可以平移,旋转和调整图形大小。

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. 但是,当我旋转或调整imageView的大小时,平移会出现问题。 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.). 例如,如果您将imageView上下颠倒,则平移手势将以反方向移动图像(向上向下,向左拖动将图像向右移动,等等)。 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). 我宁愿将此代码保存在Sprite类中而不是ViewController中(如果可能)。 Thank you. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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