iOS:将视图转换为圆柱形状 [英] iOS: transforming a view into cylindrical shape

查看:99
本文介绍了iOS:将视图转换为圆柱形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Quartz 2D,我们可以在xyz轴上变换视图.

With Quartz 2D we can transform our views on the x, yand z axis.

在某些情况下,我们甚至可以通过更改矩阵的值使它们看起来为3D.

In some cases we could even make them look 3D by changing the values of the matrixes.

我想知道是否有可能将视图转换为下图所示的圆柱形状?

I was wondering if it could be possible to transform a view into a cylinder shape like in the following picture?

请忽略圆柱体的顶部.我更想知道是否有可能像图像中的圆柱体侧面一样弯曲UIView.

Please ignore the top part of the cylinder. I am more curious to know whether it would be possible warping an UIView around like the side of the cylinder as in the image.

是否只能利用Quartz 2D,图层和变换(而不是OpenGL)?如果不是,是否可以至少在CGContext中绘制它以使视图看起来像这样?

Is that possible only making use of Quartz 2D, layers and transformations (not OpenGL)? If not, is it possible to at least draw it in CGContext to make a view appear like so?

推荐答案

您绝对不能通过转换来做到这一点.您可以做的是在屏幕外创建UIView,获取视图的上下文,从中获取图像,然后使用非线性映射将图像映射到新图像.

You definitely can't do this with a transform. What you could do is create your UIView off-screen, get the context for the view, get an image from that, and then map the image to a new image, using a non-linear mapping.

所以:

  1. 使用UIGraphicsBeginImageContext()创建图像上下文
  2. 使用view.layer.renderInContext()渲染该视图
  3. 使用CGBitmapContextCreateImage()获取结果图像
  4. 编写一个映射函数,该函数将获取x/y屏幕坐标并将其映射到圆柱体上的坐标.
  5. 创建一个与屏幕视图大小相同的新图像,然后调用映射 用于将像素从源复制到目标的功能.
  6. 在屏幕上绘制目标位图.
  1. Create an image context with UIGraphicsBeginImageContext()
  2. Render the view there, with view.layer.renderInContext()
  3. Get an image of the result with CGBitmapContextCreateImage()
  4. Write a mapping function that takes the x/y screen coordinates and maps them to coordinates on the cylinder.
  5. Create a new image the size of the screen view, and call the mapping function to copy pixels from the source to the destination.
  6. Draw the destination bitmap to the screen.

这些步骤都不是特别困难,您可能会想出各种简化方法.例如,如果可以不进行透视转换,则可以只渲染原始视图的条带,基于圆的坐标偏移Y坐标.

None of these steps is particularly-difficult, and you might come up with various ways to simplify. For example, you can just render strips of the original view, offsetting the Y coordinate based on the coordinates of a circle, if you are okay with not doing perspective transformations.

如果您希望视图实际上是交互式的,那么在处理触摸事件时就需要朝相反的方向进行转换.

If you want the view to actually be interactive, then you'd need to do the transform in the opposite direction when handling touch events.

这篇关于iOS:将视图转换为圆柱形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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