我的手势识别器存在一个小问题。
我有一个名为“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中(如果可能)。谢谢。
答案 0 :(得分:13)
而不是translationInView:self,尝试translateInView:self.superview。