我已经应用了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]
发布于 2013-03-19 15:37:40
试试这个:
- (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];
}
}
}我认为问题在于你在迭代的时候改变了数组。
https://stackoverflow.com/questions/15492298
复制相似问题