【问题标题】:touchesMoved:withEvent: stops to respondtouchesMoved:withEvent: 停止响应
【发布时间】:2013-03-19 05:45:44
【问题描述】:

我已应用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]

【问题讨论】:

  • 你想删除 self.contentView ??
  • MyCustomView userintraction 是否启用?
  • 您的 contentView 中有多少 MyCustomView 视图?此方法在主线程上调用,因此执行繁重的计算将使应用程序在方法终止之前似乎没有响应。
  • 我认为您可以删除父视图。因此,没有 self.contentView 可以添加您的新视图。这可能是您错误的原因。如果您不想删除此父视图,请使用 if 语句来传递它。
  • UITouch *touch = [[event allTouches] anyObject]; 应该改为 UITouch *touch = [touches anyObject];

标签: ios objective-c uiview touchesmoved uiresponder


【解决方案1】:

试试这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2013-04-29
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多