开发者

UIView Disappears after pan gesture

开发者 https://www.devze.com 2023-02-01 18:47 出处:网络
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?

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

精彩评论

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

关注公众号