如何调整视图大小,同时按比例调整其子视图的大小?

时间:2015-12-13 06:54:30

标签: ios swift uiview autolayout

我正在尝试创建一个自定义segue,其中第一个视图控制器缩小一点,第二个视图控制器滑过它顶部。以下是我对segue实现的看法:

override func perform() {
    let firstView = self.sourceViewController.view as UIView!
    let secondView = self.destinationViewController.view as UIView!
    let screenSize = UIScreen.mainScreen().bounds.size
    let window = UIApplication.sharedApplication().keyWindow

    secondView.frame = CGRectMake(0.0, screenSize.height, screenSize.width, screenSize.height)
    window?.insertSubview(secondView, aboveSubview: firstView)

    UIView.animateWithDuration(2, delay: 0, options: .CurveEaseIn, animations: {

        // BEGIN RELEVANT PORTION
        let offset: CGFloat = 20

        firstView.frame = CGRectMake(offset, offset, screenSize.width - (offset * 2), screenSize.height - (offset * 2))
        firstView.autoresizesSubviews = true
        firstView.layoutIfNeeded()
        // END RELEVANT PORTION

    }, completion: nil)

    UIView.animateWithDuration(5, delay: 1, options: .CurveEaseOut, animations: {

        secondView.frame = CGRectMake(0.0, 0.0, screenSize.width, screenSize.height)

    }, completion: nil)
}

这会产生如下结果:

gif showing the result of the above code

这已经非常棒了,但不是我想要的。我需要子视图(“嗨,我是马特。”标签和按钮)按比例调整超级视图的大小,就好像整个视图都落在 z 轴上一样。

我正在使用自动布局。

提前非常感谢你!

4 个答案:

答案 0 :(得分:2)

我使用CGAffineTransform解决了问题。

override func perform() {
    let firstView = self.sourceViewController.view as UIView!
    let secondView = self.destinationViewController.view as UIView!
    let screenSize = UIScreen.mainScreen().bounds.size
    let window = UIApplication.sharedApplication().keyWindow

    secondView.frame = CGRectMake(0.0, screenSize.height, screenSize.width, screenSize.height)
    window?.insertSubview(secondView, aboveSubview: firstView)

    firstView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
    firstView.layer.position = CGPoint(x: 0.0, y: 0.0)

    UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseInOut, animations: {

        print(firstView.frame)

        let offset: CGFloat = 25
        let offsetX = 2 * (offset / firstView.frame.width)
        let offsetY = 2 * (offset * (firstView.frame.height / firstView.frame.width) / firstView.frame.height)

        let transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - offsetX, 1 - offsetY)
        firstView.transform = transform
        firstView.layer.position = CGPoint(x: offset, y: offset)

    }, completion: nil)

    UIView.animateWithDuration(0.5, delay: 0.05, options: .CurveEaseOut, animations: {

        secondView.frame = CGRectMake(0.0, 0.0, screenSize.width, screenSize.height)

        }) { finished -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
    }
}

enter image description here

感谢大家的帮助!

答案 1 :(得分:2)

应用变换是实现它的一个很好的视角。

还有另一种方法可以实现它,而不会有修改第一个视图控制器视图框架的风险。拍摄第一个viewcontroller视图的快照,然后使用它而不是视图本身。您可以在 UIView 中使用以下方法拍摄快照:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;

答案 2 :(得分:1)

我在Xcode中使用您的方案尝试了,现在您必须按照这些步骤进行操作

getShort()
  

重要提示:

     

希望它会起作用,因为那时你必须给予领先& amp;尾随空间到超级视图使InnerViews缩小或扩展其超级视图

答案 3 :(得分:1)

一种方法可以是:

  • 给它们(标签和按钮)宽度约束。
  • 制作两个宽度限制的出口。 constraintWidthLabel constraintWidthButton
  • 您可以像这样制作约束

enter image description here

  • 执行segue时,必须使用动画将这些宽度约束设为零。

    UIView.animateWithDuration(2, delay: 0, options: .CurveEaseIn, animations: {
    
    // BEGIN RELEVANT PORTION
    let offset: CGFloat = 20
    constraintWidthLabel.constant = 0
    constraintWidthButton.constant = 0;
    
    firstView.frame = CGRectMake(offset, offset, screenSize.width - (offset * 2), screenSize.height - (offset * 2))
    firstView.autoresizesSubviews = true
    firstView.layoutIfNeeded()
    // END RELEVANT PORTION
    
    }, completion: nil)