我试图使用UIPanGestureRecognizer来移动我的物体,但我似乎无法让它顺利移动。当我向特定方向移动并向相反方向移动时,它不会立即作出反应。我假设值可能有问题,因为它在“ - >”中是正数。方向和否定的“< - ”。
我的代码如下:
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint pointA = [panRecognizer locationInView:self.view];
CGPoint pointB = [panRecognizer translationInView:self.view];
if(panRecognizer.state == UIGestureRecognizerStateEnded ||
panRecognizer.state == UIGestureRecognizerStateChanged) {
_camera.x += pointB.x * 0.0001f;
}
}
有没有人有更合适的方法来解决这个问题?
提前致谢。
答案 0 :(得分:6)
您应该重置UIPanGestureRecognizer的翻译价值:
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint point = [panRecognizer translationInView:self.view];
_camera.x += point.x * 0.0001f;
[panRecognizer setTranslation:CGPointZero inView:self.view];
}
答案 1 :(得分:-1)
斯威夫特3:
func panDetected(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: recognizer.view)
_camera.x += translation.x
_camera.y += translation.y
recognizer.setTranslation(CGPoint.zero, in: recognizer.view)
}
对我来说效果不错,而不会乘以0.0001f :)