开发者

UIView Disappears after pan gesture

I am using the following handler for a IUPanGesture. However when the pan ends, the UIView that it is moving disappears. Do I need to add anything else to this code?

-开发者_StackOverflow中文版 (void)pan:(UIPanGestureRecognizer *)gesture
{
  if ((gesture.state == UIGestureRecognizerStateChanged) ||
  (gesture.state == UIGestureRecognizerStateEnded)) {

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

  [self setCenter:location];
  }
}


I don't know if it's documented anywhere, but I've found empirically that any gesture states other than UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, and UIGestureRecognizerStateRecognized will have garbage touch and location information. In many cases, the code would even crash trying to access a non-existent touch.

So, changing the condition as follows should fix the issue you're having:

- (void)pan:(UIPanGestureRecognizer *)gesture
{
  if (gesture.state == UIGestureRecognizerStateChanged) {
    CGPoint location = [gesture locationInView:[self superview]];
    [self setCenter:location];
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜