Kev*_*ers 7 iphone cocoa-touch objective-c uiscrollview
请有人帮忙排序一个菜鸟吗?我已经在各种论坛上发布了这个问题并且没有得到任何答案,而许多搜索其他答案已经发现了stackOverflow,所以我希望这就是这个地方.
我有一个BeachView.h(UIScrollView的子类,沙滩的图片),随机数量为Stone.h(UIImageView的子类,一个石头的随机PNG,userInteractionEnabled = YES接受触摸).
如果用户触摸并在海滩上移动,则应滚动.如果用户点击一块石头,它应该调用方法"touchingStone".如果用户点击没有石头的海滩,则应调用方法"touchingBeach".
现在,我意识到这听起来很简单.每个人和每件事都告诉我,如果UIScrollView上有某些东西接受它应该将控制传递给它的触摸.所以当我触摸并拖动时,它应该滚动; 但如果我点击它,它在石头上,它应该忽略海滩水龙头并接受石头水龙头,是吗?
但是,似乎两个视图都接受了水龙头并且同时调用了触摸的石头和触摸的海滩.此外,沙滩水龙头首先发生,所以我甚至不能放入"如果触摸石头然后不运行touchBeach"类型的标志.
这是一些代码.在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];
}
Run Code Online (Sandbox Code Playgroud)
在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];
}
Run Code Online (Sandbox Code Playgroud)
石头水龙头之后,My NSLog看起来像这样:
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched Stone = <Stone: 0x1480c0>
ran touchedStone
Run Code Online (Sandbox Code Playgroud)
所以它实际上都在运行.更奇怪的是,如果我将touchesBegan和touchesEnded从Stone.m中删除但是留下userInteractionEnabled = YES,则beachView寄存器会触及它自己,但返回Stone作为它触摸的视图(第二次).
Touched beach = <BeachView: 0x1276a0>
ran touchedBeach
Touched beach = <Stone: 0x1480c0>
ran touchedBeach
Run Code Online (Sandbox Code Playgroud)
所以请,我一直在努力解决这个问题.我如何制作这样一个敲击的石头只调用触摸的石头和一个轻触的海滩叫只触摸了海滩?我哪里错了?
在 iPhone OS 3.0 之前,UIScrollView 的 hitTest:withEvent: 方法始终返回 self,以便直接接收 UIEvent,只有在确定与滚动或缩放无关时才将其转发到适当的子视图。
我无法对 iPhone OS 3.0 发表评论,因为它处于 NDA 之下,但请查看您的“iPhone OS 3.0 beta 5 的 iPhone SDK 发行说明”:)
如果您需要针对 3.0 之前的版本,您可以在 BeachView 中覆盖 hitTest:withEvent: 并设置一个标志,以忽略下一次海滩触摸(如果 CGPoint 实际上位于石头中)。
但是您是否尝试过简单地将对 [super Touchs*:withEvent:] 的调用从重写方法的末尾移动到开头?这可能会导致首先发生石击。
| 归档时间: |
|
| 查看次数: |
19730 次 |
| 最近记录: |