2015-12-13 146 views
0

我试图创建一个自定义的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

这已经是真正伟大的,但不是我的本意。我需要子视图(“嗨,我是马特”标签和按钮)与超视图成比例调整大小,就好像整个视图在轴上下降一样。

我正在使用自动布局。

非常感谢你提前!

+0

你想标记和按钮相对于它的父缩水? –

+0

@MuhammadWaqasBhati是的,这就是我想要做的。 :) – blerch

+0

你是否在标签和按钮上添加了超级视图约束? –

回答

1

我使用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

我试着用你的场景在我的Xcode,现在你必须遵循这些步骤

1- Give UIButton & UILabel Leading and Trailing Space to their SuperView 
2- Make UIButton central Vertically 
3- Give Vertical Space from UIButton to UILabel 

重要:

希望它会工作,因为那么诀窍是,你必须给带领&尾随空间超级查看,使InnerViews收缩或扩大其超级视图

1

单程可以是:

  • 给他们两个(标签和按钮)的宽度约束。
  • 制作两个宽度约束的出口。 constraintWidthLabelconstraintWidthButton
  • 您可以约束的出口这样

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

应用转换是实现它的好观点。

还有另一种方法可以实现它,而不会修改第一个viewcontroller视图的框架。拍摄第一个视图控制器视图的快照,并使用它来代替视图本身。您可以使用以下方法的UIView拍摄快照:

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