2017-09-14 23 views
0

我有(2)目標:導航到新的視圖控制器後,動畫在斯威夫特完成3.0

  1. 示例#1:從原來的視圖控制器導航到一個新的SecondViewController以下動畫在完成後,迅速3.0(即,用戶不必按任何按鈕和App簡單地移動到SecondViewController動畫完成後)

  2. 實施例#2:從原始視圖控制器導航到新的SecondViewController一個時間延遲之後在Swift 3.0中5秒(即用戶不必按任何按鈕和應用程序si mply移動到SecondViewController計時器之後到達5秒 - 沒有動畫涉及第二示例中,只是一個定時器)

這是我的對實施例#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

回答

0

使用DispatchQueue.main.asyncAfter

EDITED

集故事板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) 
} 
+0

由於@Kosuke小川 - 一個後續問題,當原始視圖控制器移動到SecondViewController我收到一個沒有內容的黑屏。你知道什麼可能是錯的嗎?謝謝 –

+0

你的SecondViewController是否有Xib/Storyboard? –

+0

如果是的話,使用'讓secondViewController = self.storyboard .instantiateViewController(withIdentifier:「SecondViewController」)?' –