请有人能帮助一个新手吗?我在不同的论坛上发布了这个问题,但没有得到答案,虽然搜索其他答案时发现了stackOverflow,所以我希望这里可以得到解答。
我有一个BeachView.h(UIScrollView子类,图片是沙滩)上覆盖着随机数量的Stone.h(UIImageView子类,一个随机的石头PNG,userInteractionEnabled = YES接受触摸)。
如果用户在海滩上触摸并移动,它应该滚动。 如果用户点击石头,它应该调用方法“touchedStone”。 如果用户点击海滩上没有石头的地方,它应该调用方法“touchedBeach”。
现在,我意识到这听起来很简单。每个人和一切都告诉我,如果在UIScrollView上有可以接受触摸的东西,那么它应该将控制权传递给它。所以当我触摸并拖动时,它应该滚动;但是如果我点击,并且它在一个石头上,它应该忽略海滩的点击并接受石头的点击,对吗?
然而,似乎两个视图都接受触摸并同时调用touchedStone和touchedBeach。此外,海滩点击先发生,因此我甚至无法放置“如果触摸了石头,则不要运行touchedBeach”类型的标志。
这是一些代码。 在BeachView.m上:
经过一次 stone tap 后,我的 NSLog 看起来像这样:
我有一个BeachView.h(UIScrollView子类,图片是沙滩)上覆盖着随机数量的Stone.h(UIImageView子类,一个随机的石头PNG,userInteractionEnabled = YES接受触摸)。
如果用户在海滩上触摸并移动,它应该滚动。 如果用户点击石头,它应该调用方法“touchedStone”。 如果用户点击海滩上没有石头的地方,它应该调用方法“touchedBeach”。
现在,我意识到这听起来很简单。每个人和一切都告诉我,如果在UIScrollView上有可以接受触摸的东西,那么它应该将控制权传递给它。所以当我触摸并拖动时,它应该滚动;但是如果我点击,并且它在一个石头上,它应该忽略海滩的点击并接受石头的点击,对吗?
然而,似乎两个视图都接受触摸并同时调用touchedStone和touchedBeach。此外,海滩点击先发生,因此我甚至无法放置“如果触摸了石头,则不要运行touchedBeach”类型的标志。
这是一些代码。 在BeachView.m上:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.decelerating) { didScroll = YES; }
else { didScroll = NO; }
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
NSLog(@"touched beach = %@", [touch view]);
lastTouch = touchLocation;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
didScroll = YES;
[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (didScroll == NO && isPaused == NO) {
[self touchedBeach:YES location:lastTouch];
}
[super touchesEnded:touches withEvent:event];
}
关于Stone.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[parent stoneWasTouched]; // parent = ivar pointing from stone to beachview
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
NSLog(@"touched stone = %@", [touch view]);
[parent touchedStone:YES location:touchLocation];
}
经过一次 stone tap 后,我的 NSLog 看起来像这样:
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched Stone = <Stone: 0x1480c0>
ran touchedStone
实际上它正在同时运行两者。更奇怪的是,如果我将Stone.m中的touchesBegan和touchesEnded移除但保留userInteractionEnabled = YES,则beachView自己注册了两个触摸,但返回的视图是Stone(第二次触摸)。
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched beach = <Stone: 0x1480c0>
ran touchedBeach
非常希望您能帮我解决这个问题,我已经尝试了好几天。如何让点击的石头只调用touchedStone,而点击的海滩只调用touchedBeach?我做错了什么吗?