swift有一句代码搞定APP引导页(图片/GIF/视频)

APP启动引导页(图片/gif/视频)


在APP启动时候设置引导页,不管图片,gif,还是视频只需要一个方法
视频引导页
swift有一句代码搞定APP引导页(图片/GIF/视频)

视频核心代码如下
URL为本地视频地址,如果为网络视频 建议预下载在本地然后下次进行播放

 init(videoURL: URL, isHiddenSkipButton: Bool) {
        let frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        super.init(frame: frame)
        self.playerController = MPMoviePlayerController.init(contentURL: videoURL)
        self.playerController.view.frame = frame
        self.playerController.view.alpha = 1.0
        self.playerController.controlStyle = .none
        self.playerController.repeatMode = .one
        self.playerController.shouldAutoplay = true
        self.playerController.prepareToPlay()
        self.addSubview(self.playerController.view)
        // 视频引导页进入按钮
        let movieStartButton = UIButton.init(frame: CGRect.init(x: 20, y: SCREEN_HEIGHT-70, width: SCREEN_WIDTH-40, height: 40))
        movieStartButton.layer.borderWidth = 1.0
        movieStartButton.layer.cornerRadius = 20
        movieStartButton.layer.borderColor = UIColor.white.cgColor
        movieStartButton.setTitle("开始体验", for: .normal)
        movieStartButton.alpha = 0.0
        movieStartButton.backgroundColor = .blue
        self.playerController.view.addSubview(movieStartButton)
        movieStartButton.addTarget(self, action: #selector(skipButtonClick), for: .touchUpInside)
        UIView.animate(withDuration: 1.0) {
            movieStartButton.alpha = 1.0
        }
    }

调用

         let urlStr = Bundle.main.path(forResource: "第三屏.mov", ofType: nil)
          let videoUrl = NSURL.fileURL(withPath: urlStr!)
          let guideView = LanchHUD.init(videoURL: videoUrl, isHiddenSkipButton: false)
          self.window?.rootViewController?.view.addSubview(guideView)

图片与GIF效果图如下 用了第三方库 SDWebImage
swift有一句代码搞定APP引导页(图片/GIF/视频)
核心代码

 init(imagesArray:[String],isHiddleBut:Bool) {
        let frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        super.init(frame: frame)
        self.imageArray = imagesArray
        if self.imageArray == nil || self.imageArray?.count == 0 {
            return
        }
        self.addScrollView(frame: frame)
        self.addSkipButton(isHiddenSkipButton: isHiddleBut)
        self.addImages()
        self.addPageControl()
    }
     func addImages() -> Void {
        guard let imageArray = self.imageArray else {
            return
        }
        for i in 0..<imageArray.count {
            let imageView = UIImageView.init(frame: CGRect.init(x: SCREEN_WIDTH * CGFloat(i), y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
//            let idString = (imageArray[i] as NSString).substring(from: imageArray[i].count - 3)
//            if idString == "gif" {
////
//
//            } else {
//                imageView.image = UIImage.init(named: imageArray[i])
//                self.pageView.addSubview(imageView)
//            }
            imageView.sd_setImage(with: nil, placeholderImage: UIImage.init(named: imageArray[i]), options: .avoidAutoSetImage, completed: nil)
            self.pageView.addSubview(imageView)
            
            // 在最后一张图片上显示开始体验按钮
            if i == imageArray.count - 1 {
                imageView.isUserInteractionEnabled = true
                let startButton = UIButton.init(frame: CGRect.init(x: SCREEN_HEIGHT*0.1, y: SCREEN_HEIGHT*0.8, width: SCREEN_WIDTH*0.8, height: SCREEN_HEIGHT*0.08))
                startButton.setTitle("开始体验", for: .normal)
                startButton.setTitleColor(UIColor.white, for: .normal)
//                startButton.setBackgroundImage(UIImage.init(named: "guide_btn_bg"), for: .normal)
                startButton.backgroundColor = .blue
                startButton.addTarget(self, action: #selector(skipButtonClick), for: .touchUpInside)
                //imageView.addSubview(startButton)
            }
        }
    }
    ```
    用UISctrollView进行承载然后封装