TwitterBirdAnimation icon indicating copy to clipboard operation
TwitterBirdAnimation copied to clipboard

Load a UIViewController instead of an image when animation is done

Open steffimueller opened this issue 9 years ago • 8 comments

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?

steffimueller avatar May 11 '15 00:05 steffimueller

+1

confile avatar May 12 '15 14:05 confile

Did you make that work?

osmantuna avatar Jun 01 '15 08:06 osmantuna

if you've added a rootViewController to UIWindow then simply add the mask to viewController.view.layer.mask

Rich2k avatar Jun 27 '15 06:06 Rich2k

can someone post an example?

i3lackstorm avatar Jul 15 '15 16:07 i3lackstorm

Have you solved? I have the same problem, I would like to make the mask on my HomeViewController You can write an example?

VailyI avatar Jul 28 '15 11:07 VailyI

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 avatar Jul 29 '15 13:07 confile

@confile - Any idea why it skips the animation and immediately loads the view when you set transparency = false?

loadingAnimationMaskCreate(false, backgroundColor: color, maskImage: maskImage)

shawnphoffman avatar Sep 16 '15 02:09 shawnphoffman

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

OxyFlax avatar Sep 08 '17 13:09 OxyFlax