0

I'm trying to do an animation in AppDelegate when starting the application. (From this tutorial: http://iosdevtips.co/post/88481653818/twitter-ios-app-bird-zoom-animation)

I would like to make the mask on my HomeViewController

This is my AppDelegate

import UIKit
import QuartzCore

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var mask: CALayer?
var imageView: UIImageView?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let imageView = UIImageView(frame: self.window!.frame)
    imageView.image = UIImage(named: "portfolioscreen")
    self.window!.addSubview(imageView)

    self.mask = CALayer()
    self.mask!.contents = UIImage(named: "BearLogo.png")!.CGImage
    self.mask!.contentsGravity = kCAGravityResizeAspect
    self.mask!.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
    self.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    self.mask!.position = CGPoint(x: imageView.frame.size.width/2, y: imageView.frame.size.height/2)
    imageView.layer.mask = mask
    self.imageView = imageView

    animateMask()

    if(NSUserDefaults.standardUserDefaults().integerForKey("visits") > 0)
    {
        var navigationController = UINavigationController(rootViewController: HomeViewController())
        window?.rootViewController = navigationController
    }
    else
    {
        window?.rootViewController = ViewController()
        NSUserDefaults.standardUserDefaults().setInteger(1, forKey: "visits")
    }
    //window?.makeKeyAndVisible()

    self.window!.backgroundColor = UIColor(red: 70/255, green: 154/255, blue: 233/255, alpha: 1)
    self.window!.makeKeyAndVisible()
    UIApplication.sharedApplication().statusBarHidden = true
    return true

}


func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func animateMask() {
     let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
     keyFrameAnimation.delegate = self
     keyFrameAnimation.duration = 1
     keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
     let initalBounds = NSValue(CGRect: mask!.bounds)
     let secondBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 90, height: 90))
     let finalBounds = NSValue(CGRect: CGRect(x: 0, y: 0, width: 1500, height: 1500))
     keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
     keyFrameAnimation.keyTimes = [0, 0.3, 1]
     keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
     self.mask!.addAnimation(keyFrameAnimation, forKey: "bounds")
}

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    self.imageView!.layer.mask = nil //remove mask when animation completes

    var homeViewController = HomeViewController(nibName: "HomeViewController", bundle: NSBundle.mainBundle())

    self.window?.rootViewController = homeViewController
    self.window!.makeKeyAndVisible()



}


}

When I start the simulator it crashes and there are errors in the bottom right corner. I write wrong?

help would be greatly appreciated

ezcoding
  • 2,974
  • 3
  • 23
  • 32
Vale
  • 1
  • 2
  • Well what are the errors? – pbush25 Jul 28 '15 at 12:23
  • 2015-07-28 14:31:57.654 Portfolio[1055:50883] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'HomeViewController'' – Vale Jul 28 '15 at 12:34
  • *** First throw call stack: ( 0 CoreFoundation 0x000000010e75da75 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000110581bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010e75d9ad +[NSException raise:format:] + 205 3 UIKit 0x000000010f583133 -[UINib instantiateWithOwner:options:] + 552 4 UIKit 0x000000010f3e1a88 -[UIViewController _loadViewFromNibNamed:bundle:] + 242 – Vale Jul 28 '15 at 12:35
  • 5 UIKit 0x000000010f3e2078 -[UIViewController loadView] + 109 6 UIKit 0x000000010f3e22e9 -[UIViewController loadViewIfRequired] + 75 7 UIKit 0x000000010f3e277e -[UIViewController view] + 27 8 UIKit 0x000000010f301509 -[UIWindow addRootViewControllerViewIfPossible] + 58 9 UIKit 0x000000010f301e40 -[UIWindow setRootViewController:] + 868 – Vale Jul 28 '15 at 12:36
  • The problem is with the line of code inside the `if` statement that checks the `NSUserDefaults`. It seems that trying to instantiate `HomeViewController` as the root view of the navigation controller is where the issue lies, and the error messages point to being unable to load the view. You may have to try something like `var navigationController = UINavigationController(rootViewController: self.storyboard.instantiateViewControllerWithIdentifier("homeVCIdentifier"))` – pbush25 Jul 28 '15 at 12:38

1 Answers1

0

please put this block of execution in the completionHandler in the animate method and try?

if(NSUserDefaults.standardUserDefaults().integerForKey("visits") > 0)
    {
        var navigationController = UINavigationController(rootViewController: HomeViewController())
        window?.rootViewController = navigationController
    }
    else
    {
        window?.rootViewController = ViewController()
        NSUserDefaults.standardUserDefaults().setInteger(1, forKey: "visits")
    }
sriram hegde
  • 2,301
  • 5
  • 29
  • 43
  • I don't understand. In AnimateMask( ) I already have this string of code – Vale Jul 28 '15 at 12:30
  • @sriram hegde is suggesting you try this inside the completion block of your animate method, not in `applicationDidFinishLaunching` – pbush25 Jul 28 '15 at 12:40