iOS 定制启动画面的动画效果



在AppDelegate中定义一些必要的属性

/// 本次启动是否要显示引导页,如果显示引导页则不显示启动动画
var isShowingGuideVC = false
/// 启动画面显示后,用来定制动画的ImageView,懒加载
lazy var launchIV:UIImageView! = UIImageView.init(frame: UIScreen.main.bounds)



在func application(application: didFinishLaunchingWithOptions) 方法中添加判断是否需要显示启动画面动画的逻辑

        // 在这个方法return的时候执行
        defer{
            // 不显示引导页
            if !isShowingGuideVC{
                // 必须使window可见,否则定制无效
                window?.makeKeyAndVisible()
                /// 加载启动图
                self.showLaunchImage()
            }
        }



加载启动图片,用于衔接启动时系统显示的启动图,在启动图的基础上做动画

    func loadLaunchImage() -> UIImage?{
        let screenSize = UIScreen.main.bounds.size
        let screenOrientation = "Portrait"
        guard let infoPlist = Bundle.main.infoDictionary else{
            return nil
        }
        guard let launchImages = infoPlist["UILaunchImages"] as? [[String:String]] else{
            return nil
        }
        for dict in launchImages{
            guard let sizeString = dict["UILaunchImageSize"] else{
                continue
            }
            guard let imageOrientation = dict["UILaunchImageOrientation"] else{
                continue
            }
            let imageSize = CGSizeFromString(sizeString)
            if imageSize != screenSize || imageOrientation != screenOrientation{
                continue
            }
            guard let imageName = dict["UILaunchImageName"] else{
                continue
            }
            return UIImage.init(named: imageName)
        }
        return nil
    }



将启动图显示到屏幕上,并显示动画

    // 延迟执行函数
    func delay(seconds:Double,block:@escaping (() -> Void)){
        
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: {
            block()
        })
    }

    /// 加载启动图
    func loadLaunchImage(){
        guard let image = self.loadLaunchImage() else{
            return
        }
        self.launchIV.image = image
        self.launchIV.frame = UIScreen.main.bounds
        launchIV.layer.setupCommonShadow()
        // AppWindow -> (UIApplication.shared.delegate! as! AppDelegate).window
        AppWindow.addSubview(launchIV)
        
        delay(second: 0.3) {
            self.showLaunchAnimation()
        }
    }
    /// 显示启动动画
    func showLaunchAnimation(){
        /// 动画效果:向屏幕左边移动整个画面
        let xOffset = -UIScreen.main.bounds.width * 1.5
        UIView.animate(withDuration: 1.6, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
            self.launchIV.layer.transform = CATransform3DMakeTranslation(xOffset, 1, 1)
        }) { (finish) in
            self.launchIV.removeFromSuperview()
            self.launchIV = nil
        }
    }




参考资料:
iOS开发-动态获取LaunchImage里的启动图和APPicon

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值