CALayer 内的 UIScrollView

发布于 2024-10-07 10:58:36 字数 665 浏览 3 评论 0原文

我正在开发某种纸牌游戏,其中我的视图对象(纸牌等)都是 CALayers。这些对象被添加为 mainBoard (CALayer)的子层,而它又是我的 mainView< 的 [UIView layer] 属性的子层/代码>。一切都很好,我可以进行命中测试,以正常通过 mainView 类的 touchesBegan:withEvent: 回调来验证哪个层被触摸,等等。

但是,我需要在 mainBoard 内有一个滚动条来显示“更大”的 CALayer,因此我尝试首先在 CAScrollLayer 内添加“更大”的 CALayer,但我注意到CAScrollLayer 并不真正滚动(不处理用户输入,也不绘制滚动条)。因此,解决方法是将 UIScrollView 直接添加到 mainView UIView。 UIScrollView 完美滚动,直到我尝试使用 [scrollView.layer addSublayer:] 向其添加“更大”层。

有谁知道如何在 CALayer 中拥有“可滚动”对象吗?

谢谢!

I'm developing some kind of card game in which my view objects (cards, etc) are all CALayers. These objects are added as sublayers of a mainBoard (a CALayer), which, in turn, is a sublayer of the [UIView layer] property of my mainView. Everything is Ok, I can do hit testing to verify which layer is being touched through the touchesBegan:withEvent: callback of the mainView class normally, and so on.

However, I need to have a scroll inside my mainBoard to show "bigger" CALayers, so I tried first adding the "bigger" CALayer inside a CAScrollLayer, but I noticed that the CAScrollLayer doesn't really scroll (doesn't handle user input, neither draws scrollbars). So, the workaround would be to add an UIScrollView directly to the mainView UIView. The UIScrollView scrolls perfectly, until I try to add the "bigger" layer to it, by using [scrollView.layer addSublayer:<bigger layer>].

Does anyone has any idea of how I can have "scrollable" objects inside a CALayer?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2024-10-14 10:58:36

nacho4d 指出了我的问题的答案。
回复标记:“更大”图层(边界大于屏幕中可用区域的图层(因此需要包含在可滚动区域中))。

解决方案是首先将“更大”层包装在 UIView 中:

UIView *wrapperView = ...
[wrapperView.layer addSublayer:<bigger_layer>];

之后,我可以将视图添加为 UIScrollview 的子视图:

[<uiscrollview> addSubview:wrapperView];

// set up scrolling properties
<uiscrollview>.contentSize = <bigger_layer>.frame.size;

问题是我试图直接将“更大”层添加为 UIScrollview 的子层相反,将其包装在 UIView 中并将其添加为子视图是有效的。

谢谢

nacho4d pointed the answer for my question.
Replying to mark: the "bigger" layer (a layer whose bounds is bigger than the area available in the screen (and so, needs to be contained in a scrollable area)).

The solution was to first wrap the "bigger" layer in a UIView:

UIView *wrapperView = ...
[wrapperView.layer addSublayer:<bigger_layer>];

afterwards, I can add the view as a subview of the UIScrollview:

[<uiscrollview> addSubview:wrapperView];

// set up scrolling properties
<uiscrollview>.contentSize = <bigger_layer>.frame.size;

The problem was that I was trying to add the "bigger" layer as a sublayer of the UIScrollview directly, instead, wrapping it in a UIView and adding it as a subview worked.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文