使用 CoreAnimation 或 OpenGL 弯曲/扭曲视图以实现轮播效果
现在我正在用一系列视图填充 UIScrollView。需要扭曲视图以使 UIScrollView 看起来像旋转木马。换句话说,当用户滚动时,它需要像一个圆圈。我以前从未做过类似的事情,但我假设 CoreAnimation 是不可能的,并且需要使用 OpenGL。如果 CoreAnimation 或 Quartz 可以做到这一点,那么我真的只需要一个关于如何扭曲视图的示例,我可以自己解决其余的问题,但我不熟悉 OpenGL。
Right now I'm populating a UIScrollView with a series of views. The views need to be warped to make the UIScrollView appear like a carousel. In other words when the user scrolls it needs to be like a circle. I've never done anything quite like this before, but I'm assuming CoreAnimation is out of the question and OpenGL needs to be used. If this is possible with CoreAnimation or Quartz then I really just need a sample on how to warp the views and I can figure the rest out myself but I'm not familiar with OpenGL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想扭曲视图,你要么需要 OpenGL,要么你可以使用 Core Animation 的 CAShapLayer,它允许你指定一个可以包含这条曲线的贝塞尔路径。但请记住,您看到的这种弯曲可能只是一种视错觉(尽管在上面的图像中它看起来像一条实际的曲线)。如果您连续获得足够多的具有正确 y 轴旋转的矩形,我认为您可以通过直接核心动画获得您正在寻找的效果。我很确定这就是苹果几年前提供的核心动画演示中的实现方式。以下是该演示视频的屏幕截图:
我稍微搞乱了视图层的变换,然后想出了这个:
我把它们放在一起演示项目,展示旋转如何响应滑块。它不使用滚动视图,因此您必须对其进行调整,但我认为您可以跟踪当前的滚动偏移并相应地应用变换。不确定它是否有帮助,但确实有帮助。
If you want to warp the views, you'll either need OpenGL or you could use Core Animation's CAShapLayer which allows you to specify a bezier path which can have this curve in it. But keep in mind that this curving you're seeing is likely just an optical illusion (though in your image above it looks like an actual curve). If you get enough rectangles with the correct y axis rotation in a row, I think you can come up with the effect you're looking for with straight Core Animation. I'm pretty sure that's how things are implemented in the Core Animation demos Apple provided a couple years ago. Here's a screenshot from the video from that presentation:
I messed around with the transform of a view's layer a little bit and came up with this:
I threw together a demo project that shows how the rotation works in response to a slider. It doesn't use a scroll view so you would have to adapt it, but I think you can track the current scroll offset and apply the transform accordingly. Not sure if it will help but there it is.
根据我的经验,获得正确的值有点棘手。提供视图透视的方法是通过操纵它的图层变换。我使用以下方法来实现类似效果的转换:
这样做是为了创建“翻页”动画,因此您可能需要适应。要使用它,请执行以下操作:
其中 Flip_page 是 UIView。干杯!
In my experience, it is a bit tricky to get the values right. The way to give a view perspective is by manipulating it's layer transform. I have used the following method to achieve the transfor for a similar effect:
This was done to create a "flip page" animation, so you may need to adapt. To use it, do the following:
where flip_page is a UIView. Cheers!