ios - 在 Swift 3.0 中动画完成后导航到新 View Controller

标签 ios swift xcode viewcontroller

我有 (2) 个目标:

  1. 示例#1:在 Swift 3.0 中完成以下动画后,从原始 View Controller 导航到新的 SecondViewController(即用户不必按任何按钮,应用程序只需在动画结束后移动到 SecondViewController 即可)动画完成)

  2. 示例#2:在 Swift 3.0 中,经过 5 秒的时间延迟后,从原始 View Controller 导航到新的 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

最佳答案

使用DispatchQueue.main.asyncAfter

已编辑

将 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)
}

关于ios - 在 Swift 3.0 中动画完成后导航到新 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46209316/

相关文章:

ios - 打开另一个带有选项卡栏和导航栏的 Storyboard

c++ - Xcode 中的缩进错误

ios - UIAlertController 不显示多行消息

ios - 开发将在所有 iPhone 设备上运行的 iPhone 应用程序

ios - 使用 Swift for iOS 实现 Google Classroom API 时遇到问题 : Possible missing bridging header file

SwiftUI 预览错误 - "Cannot preview in this file - Update failed"

swift - 使用 Decodable 通过 Firestore 获取对象和文档 ID

iphone - UIAlertViewDelegate 的 clickedButtonAtIndex 后崩溃并显示消息 [MPMoviePlayerViewController isKindOfClass :]: message sent to deallocated instance

ios - 如何对我的 iOS 应用程序图像进行 PngCrush?

ios - Material 设计 snackbar 在 swift 4 中不起作用