touchesMoved:withEvent:停止响应

时间:2013-03-19 05:45:44

标签: ios objective-c uiview touchesmoved uiresponder

我已应用this stackoverflow entry,以便在从一个视图移动到另一个视图时检测到触摸。

子视图确实可以检测到触摸。但是,我正在做更多的事情。我需要检测到要从其父视图中删除的触摸的视图,并将新视图添加到父视图。所以,我有这样的事情:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {
            [view removeFromSuperview];
            UIView *aNewView = [[[UIView alloc] initWithFrame:someFrame] autorelease];
            [self.contentView addSubview:aNewView];
        }
    }
}

在通过if语句后,应用程序无法响应触摸移动。您认为这个问题是什么?是由它造成的:[view removeFromSuperview]

1 个答案:

答案 0 :(得分:0)

试试这个:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    NSArray *subviews = [self.contentView.subviews copy];
    for (UIView *view in subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {
            [view removeFromSuperview];
            UIView *aNewView = [[[UIView alloc] initWithFrame:someFrame] autorelease];
            [self.contentView addSubview:aNewView];
        }
    }
}

我认为问题是你在迭代它时正在改变数组。