Forked from andrewroycarter/gist:d7597ddb396c703c8934dc080d753d1e
Created
June 7, 2017 19:49
-
-
Save DevAndArtist/ee36db341c37b8726e4af651c573771b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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