0

我已经应用了这个 stackoverflow 条目,以便在触摸从一个视图移动到另一个视图时检测到触摸。

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

- (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]

4

1 回答 1

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];
        }
    }
}

我认为问题是您在迭代数组时正在更改数组。

于 2013-03-19T07:37:40.083 回答