【发布时间】:2013-03-13 08:50:50
【问题描述】:
是否可以同时处理 UIScrollView 的滚动事件和 UIView 后面的触摸事件? 我发现 UIView 只在 UIScrollView 禁用自己的用户交互时处理它的触摸事件。
【问题讨论】:
标签: uiview uiscrollview scroll touch overlay
是否可以同时处理 UIScrollView 的滚动事件和 UIView 后面的触摸事件? 我发现 UIView 只在 UIScrollView 禁用自己的用户交互时处理它的触摸事件。
【问题讨论】:
标签: uiview uiscrollview scroll touch overlay
首先在您的视图中添加滚动视图和 in.h 文件写入委托<UIGestureRecognizerDelegate,UIScrollViewDelegate>
在你看来DidLoad 写
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
scrl.scrollEnabled = YES; // scrl is my UIScrollView
scrl.delegate = self;
scrl.contentSize=CGSizeMake(320, 680);
[self.view addSubview:scrl];
并在 handleSingleTap 方法中编写您的代码。它也会滚动,如果您点击视图,则会调用 handleSingleTap 方法。
【讨论】:
在让 UIScrollView 覆盖 SpriteKit 视图时,我遇到了类似的问题,以便用户可以滚动游戏内的大菜单。滚动视图“窃取”了所有点击,我无法按下滚动视图后面的按钮,例如我的“后退按钮”。我在滚动视图中也有按钮,在我原来的 sprite kit 场景中我有处理程序 - 我讨厌移动到我的可滚动视图中。
因此,我通过简单地将所有触摸事件发送到超类和超视图来解决此问题。 我不确定是否还有什么大的缺点,但我现在可以触摸和滚动。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesCancelled:touches withEvent:event];
[super touchesCancelled:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
【讨论】: