将桶形失真应用于 UIView

发布于 2024-08-10 00:08:45 字数 468 浏览 3 评论 0原文

我有一个 UIScrollview 填充了一个横向屏幕,其中包含一个 UIView 的小树,我可以水平滚动它。我想对滚动视图应用桶形扭曲,以便当子视图从滚动视图的外部移动到中心时,它们会改变形状。

应用这样的扭曲有什么好处呢?

有没有办法覆盖滚动视图的drawRect,绘制到位图上,扭曲它,然后将其绘制到ScrollView的上下文中?或者是否有内置 API 可以实现这种失真?

桶形失真
(来源:arielnet.com

I've got a UIScrollview which fills a landscape screen, which contains a small tree of UIViews around inside it which I can scroll horizontally. I'd like to apply a barrel distortion to the scrollview, so that as the subviews move from the outside to the centre of the scrollview, they change shape.

What is a good what to go about applying distortions like this?

Is there a way to override drawRect for the scroll view, draw onto a bitmap, distort it, then draw that to the ScrollView's context instead? Or are there built in APIs to do this sort of distortion?

barrel distortion
(source: arielnet.com)

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

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

发布评论

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

评论(1

感性 2024-08-17 00:08:45

通过正确转换每个 UIView 的图层,您可以接近这种效果。使用 CATransform3D,您可以以 3D 方式旋转和平移每个图层,以及应用透视效果。对于您的情况,您可以平移每个 UIView 层,使它们像一个旋转木马,中间视图向前并直接位于屏幕上,侧面层从屏幕倾斜并稍微凹进。这不会导致图层本身的曲率(它们仍将绘制为矩形),但如果您在主视图的图层上应用变换以创建透视效果,您应该能够非常接近。

对于这种 3D 图层定位的示例,我推荐 这个示例,其中一个完整的 3-D 迷宫是由 CALayers 构建的。

就滚动视图而言,您可能必须为此进行自定义触摸处理。您也许可以通过响应诸如scrollViewDidScroll:之类的UIScrollViewDelegate方法来完成,但我自己还没有尝试过。

You could get close to this effect by properly transforming the layers for each of your UIViews. Using a CATransform3D, you can rotate and translate each of the layers in 3-D, as well as apply a perspective effect. For your case, you can translate each of your UIView layers so that they are like a carousel, with the middle view forward and straight-on to the screen, and the side layers tilted and recessed slightly from the screen. This would not cause a curvature of the layers themselves (they will still be drawn as rectangles), but if you apply a transform on the main view's layer to create a perspective effect you should be able to get very close.

For an example of this kind of 3-D positioning of layers, I recommend this example where a full 3-D maze is constructed out of CALayers.

As far as the scroll view goes, you probably will have to do custom touch handling for this. You might be able to get by with respinding to the UIScrollViewDelegate methods like scrollViewDidScroll:, but I've not tried this myself.

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