Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a UIViewController instead of an image when animation is done #8

Open
steffimueller opened this issue May 11, 2015 · 8 comments
Open

Comments

@steffimueller
Copy link

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?

@confile
Copy link

confile commented May 12, 2015

+1

@osmantuna
Copy link

Did you make that work?

@Rich2k
Copy link

Rich2k commented Jun 27, 2015

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

@i3lackstorm
Copy link

can someone post an example?

@VailyI
Copy link

VailyI commented Jul 28, 2015

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

@confile
Copy link

confile commented Jul 29, 2015

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


}

@shawnphoffman
Copy link

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

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

@OxyFlax
Copy link

OxyFlax commented Sep 8, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants