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];
}
}
精彩评论