Iphone 平移手势会根据旋转方向弄乱方向

Iphone 平移手势会根据旋转方向弄乱方向,iphone,ios,xcode,ipad,uigesturerecognizer,Iphone,Ios,Xcode,Ipad,Uigesturerecognizer,我的手势识别器有一个小问题 我有一个叫做“Sprite”的类,它只是一个UIImageView。Sprite有自己的手势识别器和处理方法,用户可以平移、旋转和调整图形大小 这是我的密码: -(void)setup{ //sets up the imageview... //add the image, frame, etc. UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithT

我的手势识别器有一个小问题

我有一个叫做“Sprite”的类,它只是一个UIImageView。Sprite有自己的手势识别器和处理方法,用户可以平移、旋转和调整图形大小

这是我的密码:

    -(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]
}
所以基本上他们每个人都能独立工作。但是,当我旋转或调整imageView的大小时,平移会出现问题。例如,如果将imageView倒置旋转,则平移手势将沿相反方向移动图像(向上是向下的,向左拖动将其向右移动,等等)。同样,调整大小的精灵也不会以与以前相同的速度/距离平移


有没有办法解决这个问题?我更愿意将这段代码保存在Sprite类中,而不是ViewController(如果可能的话)。谢谢。

请尝试TranslationView:self,而不是TranslationView:self.superview

Txs@Jerry,回答得很好。这简直让我发疯了!