ios - 旋转视图和子视图
问题描述
我在 iOS 9+ 应用程序中旋转带有子视图的图像时遇到问题。我有一个包含 2 个子视图的容器视图。子视图与容器视图大小相同。第一个子视图包含来自 PDF 页面的图像。第二个子视图包含 UIImageViews 作为位于 PDF 图像顶部的子视图。我使用 PDF 的坐标系来正确放置和调整图像视图的大小。(也许我应该提到容器视图本身就是 UIScrollview 的子视图)。
无论 PDF 是纵向还是横向,图像视图都已正确放置和定向。但是,当 PDF 为横向时,我想旋转和缩放最终图像以使其正常显示。我可以做到这一点的一种方法是分别旋转、转换和缩放 PDF 和每个图像视图,将绘图代码放在每个视图的 drawRect 方法中。这行得通,但它真的很慢。
我从 SO 帖子中了解到,如果我将旋转和变换应用于容器视图的 CALayer,iOS 会将更改应用于整个视图层次结构。旋转横向图像时,这运行得更快。但是我无法使用容器视图的图层来缩放最终图像。在 iPad 上,我最终得到一个正确旋转的最终图像,在屏幕顶部水平居中,但在左侧和右侧被剪裁。图像的长轴仍然等于屏幕的高度,宽于屏幕的宽度。
容器视图中的代码非常短:
- (void) setOrientation:(NSInteger)orientation
{
_orientation = orientation;
if (orientation == foPDFLandscape)
{
// [[self layer] setNeedsDisplayOnBoundsChange:YES];// no effect
// [[self layer] setBounds:CGRectMake(0.0, 0.0, 100.0, 100.0)];//does not change image size or scale
// [[self layer] setContentsScale:0.5];//does not change image size or scale
[[self layer] setAnchorPoint:CGPointMake(0.0, 0.9)];
CATransform3D transform = CATransform3DMakeRotation(90.0 * (M_PI / 180.0), 0.0, 0.0, 1.0);
[[self layer] setTransform:transform];
//putting the scaling code here instead of before the transform makes no difference
}
}
在变换之前或之后以及以各种组合设置边界、帧或 contentsScale,对最终图像没有影响。也不会更改内容重力值和 autoResizeMask。
有没有办法我可以做到这一点?
谢谢
解决方案
我需要像这样连接转换:
- (void) setOrientation:(NSInteger)orientation
{
_orientation = orientation;
if (orientation == foPDFLandscape)
{
[[self layer] setAnchorPoint:CGPointMake(0.0, 1.0)];
CATransform3D rotate = CATransform3DMakeRotation(90.0 * (M_PI / 180.0), 0.0, 0.0, 1.0);
CATransform3D scale = CATransform3DMakeScale(0.77, 0.77, 1.0);
CATransform3D concat = CATransform3DConcat(rotate, scale);
[[self layer] setTransform:concat];
}
}
但这是部分解决方案。最终图像被裁剪为屏幕大小——在 iPad 上可以,但在 iPhone 上不行。此外,图像不再响应缩放手势。
推荐阅读
- python - pandas groupby 同时加法和平均法
- sql - 查询有效但无法检索数据
- docusignapi - Docusign API:同一仪式中的多个签名
- jquery - 从 Gravity Forms 上的同意输入中获取价值
- rust - 如何为迭代器编写具有生命周期的特征和实现?
- python - 当字符串中有多个逗号时,保留字符串中每个逗号后的第一个单词
- html - MacOS Safari 输入多、对焦慢、打字迟缓
- python - 如何使用 SqlAlchemy ORM 更改列标签
- python - 将 DataFrame 列的字符串转换为其他列,如字典
- javascript - 如何使用 JavaScript 的 FileReader API 读取多个文本文档并将其内容作为字符串添加到数组中?