如何改变视图比例

how to change the scale of view

我正在尝试制作类似于 Euro Sport 应用程序中的侧边栏菜单!当菜单从左边滑动时,sourceviewcontroller向左滑动变小。

var percentWidthOfContainer = containerView.frame.width * 0.2 // this is 20 percent of width

    var widthOfMenu = containerView.frame.width - percentWidthOfContainer

    bottomView.transform = self.offStage(widthOfMenu)

    bottomView.frame.origin.y = 60

    bottomView.frame.size = CGSizeMake(widthOfMenu, 400)

    bottomView.updateConstraints()

    menucontroller.view.frame.size = CGSizeMake(widthOfMenu, containerView.frame.height)

    menucontroller.updateViewConstraints()

这里,仰视图是sourceviewcontroller.view。所以,问题是如何缩放底视图。在我的例子中,我可以改变大小,但视图中的所有内容仍然是相同的大小。

根据 Apple 文档

The CGAffineTransform data structure represents a matrix used for affine transformations. A transformation specifies how points in one coordinate system map to points in another coordinate system. An affine transformation is a special type of mapping that preserves parallel lines in a path but does not necessarily preserve lengths or angles. Scaling, rotation, and translation are the most commonly used manipulations supported by affine transforms, but skewing is also possible.

所以你的解决方案是最小化 bottomView 的大小:-

bottomView.transform = CGAffineTransformMakeScale(0.2, 0.2) // you can change it as per your requirement 

如果你想调整它的大小或最大化它到原来的大小:

bottomView.transform = CGAffineTransformMakeScale(1.0, 1.0) 

以防万一您想将底视图展开得超过其大小:-

 bottomView.transform = CGAffineTransformMakeScale(1.3, 1.3) // you can change it as per your requirement 

您可以使用 CGAffineTransformScale 来扩展 UIView

的实例

例如

假设您有一个 UIView 的实例作为

UIView *view;

// 假设您已经实例化并自定义了您的视图

.. ..

// 将视图的原始变换保存在一个变量中作为

CGAffineTransform viewsOriginalTransform = view.transform;

// 缩小视图使用 CGAffineTransformScale

view.transform = CGAffineTransformScale(viewsOriginalTransform, 0.5, 0.5);

//再次放大视图

view.transform = CGAffineTransformScale(viewsOriginalTransform, 1.0, 1.0);