在更改目标的超级视图后检测TouchesEnded

时间:2012-05-16 15:13:10

标签: objective-c ios uiview uiscrollview uipangesturerecognizer

在iOS中,我在两个父UIView之间拖动UIView。我正在使用UIPanGestureRecognizer来管理拖动。当拖动仍在进行时,我使用以下方法切换要拖动的视图的父级:

- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
    ...
    [viewBeingDragged removeFromSuperview];
    UIView* page2 = [self.view viewWithTag:pageTag];
    [page2 addSubview:viewBeingDragged];
    // Now scroll page 2 into view
    [pageContainerScrollView setContentOffset:CGPointMake(xcoord, 0) animated:YES];
    ...

这似乎终止了平移事件。然而,我的手指仍然“向下”。在这一点上,我怎么能检测到我的手指抬起?我已经尝试了一个单独的UITapGestureRecognizer,如果没有发生平移则会触发,但如果平移已经开始则不会触发。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        ...
    }

    UIPanGestureRecognizer *recognizer;
    recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];        
    [self addGestureRecognizer:recognizer];
}

-(void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

    if ( [recognizer state] == UIGestureRecognizerStateEnded )
    {
        NSLog(@"Ended");
    }
}

您可以找到所有州here

希望它有所帮助。

答案 1 :(得分:0)

我有same problem。您只需删除该行:[viewBeingDragged removeFromSuperview];

UIView只能有一个超级视图,因此当您致电[page2 addSubview:viewBeingDragged]时,viewBeingDragged UIView将首先从其当前的超级视图中删除。