Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// ViewController.swift
// test
//
// Created by Andrew Carter on 6/7/17.
// Copyright © 2017 Andrew Carter. All rights reserved.
//
import UIKit
class PushOperation: Operation {
private let viewController: UIViewController
private let view: UIView
private var didAnimateInViewController = false
private var isAnimatingViewController = false
init(viewController: UIViewController, for view: UIView) {
self.viewController = viewController
self.view = view
}
override var isFinished: Bool {
return didAnimateInViewController
}
override var isAsynchronous: Bool {
return true
}
override var isExecuting: Bool {
return isAnimatingViewController
}
override func start() {
willChangeValue(forKey: "isExecuting")
isAnimatingViewController = true
didChangeValue(forKey: "isExecuting")
DispatchQueue.main.async {
UIView.animate(withDuration: 2.0, animations: {
// Add you view controller anmiation code here, I'm just doing this
// for the sake of something happening
self.view.addSubview(self.viewController.view)
self.viewController.view.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
print("Animating in \(self.viewController.title ?? "viewController")")
}, completion: { _ in
self.willChangeValue(forKey: "isFinished")
self.didAnimateInViewController = true
self.didChangeValue(forKey: "isFinished")
print("Finished animating in \(self.viewController.title ?? "viewController")")
})
}
}
}
class ViewController: UIViewController {
let operationQueue: OperationQueue = {
var operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
return operationQueue
}()
func push(controller: UIViewController) {
operationQueue.addOperation(PushOperation(viewController: controller, for: view))
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let viewControllerOne = UIViewController()
viewControllerOne.view.backgroundColor = UIColor.blue
viewControllerOne.title = "One"
let viewControllerTwo = UIViewController()
viewControllerTwo.view.backgroundColor = UIColor.red
viewControllerTwo.title = "Two"
let viewControllerThree = UIViewController()
viewControllerThree.view.backgroundColor = UIColor.green
viewControllerThree.title = "Three"
push(controller: viewControllerOne)
push(controller: viewControllerTwo)
push(controller: viewControllerThree)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment