【问题标题】:Create sliding animation in View Swift在 View Swift 中创建滑动动画
【发布时间】:2016-07-04 04:54:06
【问题描述】:

我想在 Swift 中制作滑动动画。我希望它通过从第一个视图控制器向下滑动来从第一个控制器转到第二个控制器。示例:您的 POV 位于 firstVC 上,当您按下按钮时,firstVC 向下滑动,其顶部是 secondVC .这是一个合理的过渡吗?如果是这样,我将如何做到这一点?谢谢!

这是我正在寻找的示例图片:Example

注意:我不希望 secondView 滑过第一个视图,我希望它在向下滑动时保持在第一个视图的上方(或顶部)。

【问题讨论】:

标签: ios swift uiview


【解决方案1】:

要创建此转场,您需要创建一个UIStoryBoardSegue 文件。 在该文件中,输入以下代码:

 override func perform() {
        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, -screenHeight, screenWidth, screenHeight)
    firstVCView.frame = CGRectMake(0.0, 0.0, screenWidth, screenHeight)

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

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

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

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

控制点击创建一个segue。选择“自定义”。然后,在属性检查器中输入 segue 文件名。您的自定义 segue 很好用!

希望这会有所帮助。

【讨论】:

  • 非常感谢!我一直在尝试解决这个问题,并且 100% 有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多