首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >touchesMoved:withEvent:停止响应

touchesMoved:withEvent:停止响应
EN

Stack Overflow用户
提问于 2013-03-19 13:45:44
回答 1查看 538关注 0票数 0

我已经应用了this stackoverflow entry,所以当它从一个视图移动到另一个视图时,我可以检测到触摸。

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

代码语言:javascript
复制
- (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]

EN

回答 1

Stack Overflow用户

发布于 2013-03-19 15:37:40

试试这个:

代码语言:javascript
复制
- (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];
        }
    }
}

我认为问题在于你在迭代的时候改变了数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15492298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档