I'm trying to implement a UIView that can be dragged out of its superview.
I tried adding a UIPanGestureRecognizer
to the view I want to be able to drag. It seems, however, that removing the UIView from its superview and adding it to another view, is breaking the gesture recognizer.
With the code within the UIGestureRecognizerStateBegan
commented out, the code within the other two blocks functions correctly, but when I reinstate it, the UIGestureRecognizerStateChanged and UIGestureRecognizerStateEnded states are never achieved.
What is going wrong?
if ([gr state] == UIGestureRecognizerStateBegan)
{
CGPoint newCenter = [endView convertPoint:[self center]
fromView:startView];
[self removeFromSuperview];
[endView addSubview:self];
[self setCenter:newCenter];
}
if ([gr state] == UIGestureRecognizerStateChanged)
{
// Some code that successfully moves the view.
}
if ([gr state] == UIGestureRecognizerStateEnded)
{
// Other code.
}
You deduce right, [self removeFromSuperview]
breaks gesture recognizer. I have had the same problem. Comment this line [self removeFromSuperview]
and should be ok, you don't have to remove it from superview, because UIView can only be a subview of one view.
来源:https://stackoverflow.com/questions/12161047/changing-superview-breaks-uipangesturerecognizer