TwitterBirdAnimation
TwitterBirdAnimation copied to clipboard
Load a UIViewController instead of an image when animation is done
I want to load a UIViewController instead of an image when animation is done.
I tried in your example's AppDelegate:
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.imageView!.layer.mask = nil //remove mask when animation completes
self.imageView?.removeFromSuperview()
println("animationDidStop")
self.window?.rootViewController = ViewController()
self.window?.makeKeyAndVisible()
}
But it did not show the controller. How can I do that?
+1
Did you make that work?
if you've added a rootViewController to UIWindow then simply add the mask to viewController.view.layer.mask
can someone post an example?
Have you solved? I have the same problem, I would like to make the mask on my HomeViewController You can write an example?
Here is an example you can use. Just make HomeViewController : IntroViewController
import UIKit
/**
A viewController which presents a Twitter-like start logo animation.
See: https://github.com/rounak/TwitterBirdAnimation
*/
// UIViewController
class IntroViewController: UIViewController {
private var loadingMask: CALayer?
private var windowColor: UIColor?
override func viewDidLoad() {
super.viewDidLoad()
let color: UIColor = ...
let maskImage: UIImage = .....
loadingAnimationMaskCreate(true, backgroundColor: color, maskImage: maskImage)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
animateLoadingMask()
}
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
if self.loadingMask != nil {
self.view.layer.mask = nil
self.loadingMask?.superlayer?.removeFromSuperlayer()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
if let windowColor = self.windowColor {
appDelegate.window!.backgroundColor = windowColor
}
self.loadingMask = nil
}
}
// MARK: Private methods
/**
Create a loading mask.
*/
private func loadingAnimationMaskCreate(transparent: Bool, backgroundColor: UIColor, maskImage: UIImage) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let maskSize: CGSize = CGSizeMake(120, 120)
self.windowColor = appDelegate.window?.backgroundColor
appDelegate.window!.backgroundColor = backgroundColor
// UIApplication.sharedApplication().keyWindow?.backgroundColor = backgroundColor
// appDelegate.window?.tintColor = UIColor.blueColor()
let screenBounds = UIScreen.mainScreen().bounds
let mask = CALayer()
mask.contents = maskImage.CGImage
mask.contentsGravity = kCAGravityResizeAspect
mask.bounds = CGRect(x: 0, y: 0, width: maskSize.width, height: maskSize.height)
mask.anchorPoint = CGPoint(x: 0.5, y: 0.5)
mask.position = CGPoint(x: screenBounds.width/2, y: screenBounds.height/2)
self.loadingMask = mask
if transparent {
self.view.layer.mask = mask
}
else {
let backgroundMask = CALayer()
backgroundMask.frame = self.view.frame
backgroundMask.backgroundColor = backgroundColor.CGColor
self.view.layer.addSublayer(backgroundMask)
backgroundMask.addSublayer(mask)
}
}
// animate the loading mask
private func animateLoadingMask() {
if self.loadingMask != nil {
let keyFrameAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
keyFrameAnimation.delegate = self
keyFrameAnimation.duration = 1
keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
keyFrameAnimation.values = [1.0, 0.9, 23.0] //scale percentages 1.0 = original size
keyFrameAnimation.keyTimes = [0, 0.3, 1]
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
self.loadingMask!.addAnimation(keyFrameAnimation, forKey: "transform.scale")
}
}
}
@confile - Any idea why it skips the animation and immediately loads the view when you set transparency = false?
loadingAnimationMaskCreate(false, backgroundColor: color, maskImage: maskImage)
Thank you @confile for your example. I had to add those lines to avoid first frame at the end of the animation:
keyFrameAnimation.fillMode = kCAFillModeBoth
keyFrameAnimation.isRemovedOnCompletion = false