在 Swift 3.0 中完成动画后导航到新视图控制器

Navigate to New View Controller after Animation is Completed in Swift 3.0

我有 (2) 个目标:

  1. 示例 #1:在 Swift 3.0 中完成以下动画后,从原始视图控制器导航到新的 SecondViewController(即用户不必按任何按钮和App只是在动画完成后移动到SecondViewController)

  2. 示例 #2:在 Swift 3.0 中,在 5 秒的时间延迟后从原始视图控制器导航到新的 SecondViewController(即用户不必按任何按钮和在计时器达到 5 秒后,应用程序简单地移动到 SecondViewController - 第二个示例中没有涉及动画,只有计时器)

这是示例 #1 的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    var imagesName = ["Image_1","Image_2","Image_3","Image_4","Image_5","Image_6","Image_7","Image_8","Image_9","Image_10","Image_11","Image_12","Image_13","Image_14","Image_15"]

    var images = [UIImage]()

    for i in 0..<imagesName.count{

        images.append(UIImage(named: imagesName[i])!)
    }

    imageView.animationImages = images
    imageView.animationDuration = 1.0
    imageView.startAnimating()

// Perhaps here is where we can fire the code to programatically move to a new View Controller after the animation is completed

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是我的 Xcode 设置:

使用DispatchQueue.main.asyncAfter

已编辑

将 Storyboard ID 设置为 SecondViewController。

示例 #1:

...
imageView.animationRepeatCount = 1 // <-- here
imageView.startAnimating()

DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {
    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
    self.show(secondViewController, sender: nil)
}

示例 #2:

...
imageView.animationRepeatCount = 1 // <-- here
imageView.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
    self.show(secondViewController, sender: nil)
}