how to change the scale of view

2019-08-01 07:32发布

问题:

I am trying to make sidebar menu like in app Euro Sport! When the menu slides from left , the sourceviewcontroller slide to left and becomes smaller.

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()

Here, the bottom view is sourceviewcontroller.view. So, the question is how to scale bottom view. In my case , i can change the size but everything inside view is still in the same size.

回答1:

You can use CGAffineTransformScale to scale instance of UIView

For Instance

Suppose you have an instance of UIView as

UIView *view;

// lets say you have instantiated and customized your view

.. ..

// Keep the original transform of the view in a variable as

CGAffineTransform viewsOriginalTransform = view.transform;

// to scale down the view use CGAffineTransformScale

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

// again to scale up the view

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


回答2:

As per Apple doc's

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.

So your solution to minimize the size of bottomView :-

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

If you want to resize it or maximize it to its original size:

bottomView.transform = CGAffineTransformMakeScale(1.0, 1.0) 

Just in case you want to expand the bottom view more than its size:-

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