平移手势会扰乱基于旋转的方向
我的手势识别器有一个小问题。
我有一个名为“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 中(如果可能的话)。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要尝试translationInView:self,而是尝试translationInView:self.superview。
Instead of translationInView:self, try translationInView:self.superview.