UIStoryboardSegue:顶视图没有向下滑动

Posted

技术标签:

【中文标题】UIStoryboardSegue:顶视图没有向下滑动【英文标题】:UIStoryboardSegue: top view is not sliding down 【发布时间】:2015-08-04 08:20:59 【问题描述】:

我有以下 swift 代码,我想要实现的是从顶部滑落的 segue。我希望 secondVCView 位于 firstVCView 下方,并且希望 firstVCView 滑出显示 secondVCView。

目前没有动画,只是闪烁到第二个VCView。

非常感谢

import UIKit

class TopToBottomSegue: UIStoryboardSegue 

override func perform() 

    // Assign the source and destination views to local variables.
    var firstVCView = self.sourceViewController.view as UIView!
    var secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0, 0, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view below the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, belowSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.4, animations:  () -> Void in

        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, screenHeight)
        //secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

        )  (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                animated: false,
                completion: nil)
    

  

【问题讨论】:

【参考方案1】:

据我所知,您需要在 animateWithduration 块中调用 layoutIfNeed 才能获得流畅的动画。

问候

【讨论】:

【参考方案2】:

我不确定这是否是正确的方法,但我设法通过插入两个视图使其工作。

L

--

import UIKit

class TopToBottomSegue: UIStoryboardSegue 

override func perform() 
    // Assign the source and destination views to local variables.
    let firstVCView = self.sourceViewController.view as UIView!
    let secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width, height and status bar height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height
    let statusBarHeight = self.statusBarHeight()

    // Specify the initial position of both views.
    secondVCView.frame = CGRectMake(0.0, 0.0, screenWidth, screenHeight)
    firstVCView.frame = CGRectMake(0.0, statusBarHeight, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view below the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(firstVCView, aboveSubview: secondVCView)
    window?.insertSubview(secondVCView, belowSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.3, animations:  () -> Void in

        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, screenHeight)

        )  (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as UIViewController,
                animated: false,
                completion: nil)
    


func statusBarHeight() -> CGFloat 
    let statusBarSize = UIApplication.sharedApplication().statusBarFrame.size
    return Swift.min(statusBarSize.width, statusBarSize.height)


【讨论】:

以上是关于UIStoryboardSegue:顶视图没有向下滑动的主要内容,如果未能解决你的问题,请参考以下文章

UIStoryBoardSegue 过渡 - 状态栏问题

UIStoryboardSegue:动画期间不显示视图

UIStoryboardSegue 嵌入

UIStoryboardSegue 为子类中的属性设置动画

准备(用于 segue:UIStoryboardSegue,发件人:任何?)在 iOS 11 上的 Xcode 9 b6 中未调用

自定义 UIStoryboardSegue 使用 segueWithIdentifier:source:destination:performHandler: