0

我正在为 IUPanGesture 使用以下处理程序。但是,当平移结束时,它正在移动的 UIView 消失了。我需要在此代码中添加任何其他内容吗?

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
  (gesture.state == UIGestureRecognizerStateEnded)) {

  CGPoint location = [gesture locationInView:[self superview]];

  [self setCenter:location];
  }
}
4

1 回答 1

1

我不知道它是否记录在任何地方,但我凭经验发现,除 , 和 之外的任何手势状态UIGestureRecognizerStateBegan都会UIGestureRecognizerStateChangedUIGestureRecognizerStateRecognized垃圾触摸和位置信息。在许多情况下,代码甚至会在尝试访问不存在的触摸时崩溃。

因此,如下更改条件应该可以解决您遇到的问题:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if (gesture.state == UIGestureRecognizerStateChanged) {
    CGPoint location = [gesture locationInView:[self superview]];
    [self setCenter:location];
  }
}
于 2010-12-26T01:51:28.990 回答