旋转视图和子视图



我在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
}
}

在变换之前或之后以各种组合设置边界、帧或内容缩放对最终图像没有影响。更改内容重力值和自动调整大小蒙版也不会。

有没有办法做到这一点?

谢谢

我需要像这样连接转换:

- (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上不行。此外,图像不再响应缩放的捏合手势。

最新更新