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

时间:2017-09-14 01:57:30

标签: ios swift xcode viewcontroller

我有(2)个目标:

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

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

  3. 以下是我的示例#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设置:

    enter image description here

1 个答案:

答案 0 :(得分:0)

使用DispatchQueue.main.asyncAfter

<强> EDITED

将Storyboard ID设置为SecondViewController。

enter image description here

示例#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)
}