문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top