如何将UIView中的触摸传递给UIScrollView [英] How to pass touches from UIView to UIScrollView

查看:135
本文介绍了如何将UIView中的触摸传递给UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于此主题还有很多其他人,我有一个带有NIB文件的视图控制器,它具有这种布局......

Similar to this thread and many others, I've got a view controller with a NIB file which has this layout...


  • a。 UIView(480 x 320) - 存储背景图像

  • b。 UIScrollView(480 x 220) - 是一个关卡选择器scrollview

  • c。 UIView(480 x 320) - 包含前景动画图形

上面的所有三个项目都是NIB中主视图的子视图。 UIView(c)是iPhone屏幕的完整大小,位于层次结构之上。在它上面,我放置了一个基于该视图中当前触摸位置动画的角色。然而问题是,在这个视图接收触摸时,我无法触及它下面的ScrollView(b)。我仍然需要使用(c)中的触摸,但之后需要将相关的触摸/滑动传递到下面的UIScrollView。

All three items above are subviews of the main View in the NIB. UIView (c) is the full size of the iPhone screen and on top of the hierarchy. On it, I've placed a character which animates based on the current touch position in that view. However the issue is that with this view receiving touches, I cannot get the touches to the ScrollView (b) below it. I still need to use the touches at (c) but also need to pass relevant touches / swipes to the UIScrollView below afterwards.

有人可以建议如何做到这一点吗?我已经阅读了关于使用hittest的各种帖子,但不想完全抵消这些触摸,我只需要转发它们,以便滚动视图仍能正常工作。

Can anyone advise how this can be done? I've read various posts on using hittest but don't want to offset the touches completely, I just need to forward them after so that the scrollview still works as normal.

谢谢,

推荐答案

如果是我,我会将scrollView设置为顶视图的委托。当然,这需要顶视图是 UIView 的自定义子级别,因此您可以设置

If it were me, I would set the scrollView as the top view's delegate. This, of course, would require that the top view is a custom sublcass of UIView, so you can set

id delegate; 

@property (nonatomic, assign) id delegate;

。然后,您可以将scrollView设置为委托,其内容为

in the header file. You would then set the scrollView as the delegate with something along the lines of

[topView setDelegate:scrollView];

有了这个,你就可以发送消息(在你的情况下,触摸事件)到scrollView什么时候有必要。例如,如果你想发送所有touchBegan事件,你就会有:

With this in place, you can then send messages (in your case, touch events) to the scrollView when they are necessary. If, for example, you want to send all touchBegan events, you would have this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.delegate touchesBegan:touches withEvent:event];
}

当然,如果你只想要通过某些事件,你可以设置你的方法中的参数说何时调用 [self.delegate ....] [super ....] 。后者将对视图执行操作,而不是在scrollView中执行操作。我希望这有助于

Of course, if you only want certain events passed, you would set up your parameters in the methods to say when to call [self.delegate ....] or [super ....]. The latter will perform the action for the view rather than in the scrollView. I hope this helps

这篇关于如何将UIView中的触摸传递给UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆