Ios 在SWIFT中启动图像闪屏动画

Ios 在SWIFT中启动图像闪屏动画,ios,swift,animation,core-animation,splash-screen,Ios,Swift,Animation,Core Animation,Splash Screen,我想在swift中为我的ios应用程序创建一个类似twitter的启动图像 我确实理解动画发射图像的一般问题,我基本上知道创建此类动画的步骤: func animateEmoji() { let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds") keyFrameAnimation.delegate = self keyFrameAnimation.duration = 1 keyFrameAnim

我想在swift中为我的ios应用程序创建一个类似twitter的启动图像

我确实理解动画发射图像的一般问题,我基本上知道创建此类动画的步骤:

func animateEmoji() {
    let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
    keyFrameAnimation.delegate = self
    keyFrameAnimation.duration = 1
    keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
    let initalBounds = NSValue(CGRect: emoji.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.emoji.addAnimation(keyFrameAnimation, forKey: "bounds")
}

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    // step 3: remove the view
    self.viewForLayer.removeFromSuperview()
}
  • 在应用程序启动后立即添加一个与launchimage内容完全相同的新视图
  • 设置该视图的动画
  • 让风景消失吧
  • 我找到了这个绝妙的来源:

    但是这个家伙使用了一个面具,我无法更改他的代码,在没有面具的情况下获得“真实的”类似twitter的动画,而只是一个动画(放大)图像

    那么,如何将新视图添加到当前视图中?如果我使用子视图,当我需要在animationDidStop中删除子视图时,如何在以后的进度中识别该视图

    哦,我很乐意在AppDelegate中完成所有这些

    以下是我目前的做法:

    在AppDelegate中设置视图:

    import UIKit
    import QuartzCore
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var viewForLayer: UIView!
        var window: UIWindow?
    
        var emoji: CALayer {
          return viewForLayer.layer
        }
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
          self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
          // step 1: recreate the launch image
    
          self.emoji.backgroundColor = UIColor(red: 52/255, green: 52/255, blue: 52/255, alpha: 0).CGColor
          self.emoji.contents = UIImage(named: "SplashScreenEmoji")!.CGImage
    
          self.emoji.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
          self.emoji.anchorPoint = CGPoint(x: 0.5, y: 0.5)
          self.emoji.position = CGPoint(x: self.window!.frame.size.width/2, y: self.window!.frame.size.height/2)
    
          // self.window!.addSubview(viewForLayer) or
          viewForLayer.layer.addSublayer(emoji)
    
          // step 2: add the animation to that view
          animateEmoji()
    
          self.window!.makeKeyAndVisible()
          UIApplication.sharedApplication().statusBarHidden = true
          return true
        }
    }
    
    对于动画:

    func animateEmoji() {
        let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
        keyFrameAnimation.delegate = self
        keyFrameAnimation.duration = 1
        keyFrameAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
        let initalBounds = NSValue(CGRect: emoji.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.emoji.addAnimation(keyFrameAnimation, forKey: "bounds")
    }
    
    override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
        // step 3: remove the view
        self.viewForLayer.removeFromSuperview()
    }
    
    我总是会遇到一个致命错误:在展开可选值时意外发现nil,我想这是因为

    var emoji: CALayer {
       return viewForLayer.layer
    }
    
    我确实陷入困境,需要stackoverflow社区的帮助,请不要判断,因为我是新来的,只要学习如何编写swift;)

    谢谢,
    jo

    在这里很难复制您的错误,但我可以找到错误的根源

    当你写作时

    var emoji: CALayer {
        return viewForLayer.layer
    }
    
    然后使用self.emoji调用此变量,或

    实际上,您正在隐式展开变量viewForLayer,该变量具有隐式展开的可选UIView类型:


    我脑海中唯一的解释是,出于某种原因,调用viewForLayer时,viewForLayer为nil,这将在隐式展开(即以任何方式使用)时准确地导致错误。我不太了解你的代码,但我看不出你在哪里初始化了viewForLayer,我的意思是给它一个非零的值。

    你不需要使用层,你可以通过
    UIView
    s和
    UIView
    动画块来实现缩放效果


    创建一个自定义视图控制器,仅在其视图中添加一个图像视图,将该视图控制器指定为窗口的
    rootViewController
    。使用
    UIView
    动画块增加图像视图的比例(通过
    CGAffineTransform
    )。在动画的完成块中,可以将实际的初始视图控制器指定给窗口的
    rootViewController

    。所以我正在使用xcode脚本。我的想法是以编程方式创建一个新的空UIView,在那里我可以添加动画。该视图是否必须存在于情节提要中的我的rootviewcontroller上?我也尝试过,但我无法将视图链接到appDelegate文件(链接的意思是创建一个IBOutlet)。我在appdelegate:var viewForLayer=UIView(frame:CGRectMake(0,0,100,100))中调整了这一行以初始化视图太棒了!这对我很有用。我只剩下一个问题了。动画结束后,此代码应显示我的初始rootview控制器:override func animationDidStop(anim:CAAnimation!,finished flag:Bool){self.window!.rootViewController=Nav1()},但由于某些原因,我只能看到Nav1 viewController上有一个非常暗的阴影,并且没有任何内容。rounak?或者其他人知道如何解决这个问题吗?@JoF.T。你为动画正确分配了代理吗?你的确切意思是什么?动画发生在AppDelegate中。我正在为AppDelegate中的动画创建一个名为“splashScreen”的UINavigationController。动画也发生在AppDelegate中。你的意思是像splashScreen.delegate=self这样的东西吗?基本上,如果你在animationDidStop中设置一个断点,Xcode会在那里中断吗?
    var viewForLayer: UIView!