美文网首页
ios 加载动画

ios 加载动画

作者: 孟小于max | 来源:发表于2019-03-27 11:30 被阅读0次

    主要是设置帧动画,楼主在这里设置了四张图片

    import UIKit
    
    class LoadAnimationView: UIView {
    
       private var isAnimating:Bool = false
       private var backImageView:UIImageView!
       private var imageView:UIImageView!
       
       override init(frame: CGRect) {
           super.init(frame: frame)
           self.backgroundColor = UIColor.clear
           backImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
           backImageView.backgroundColor = UIColor.clear
           self.addSubview(backImageView)
           
           self.imageView = UIImageView.init(frame: CGRect(x: frame.size.width / 2 - 120, y: frame.size.height / 2 - 50, width: 200, height: 73))
           backImageView.addSubview(imageView)
           
           imageView.animationImages = [
               UIImage.init(named: "jiazai1Image"),
               UIImage.init(named: "jiazai2Image"),
               UIImage.init(named: "jiazai3Image"),
               UIImage.init(named: "jiazai4Image")
               ] as? [UIImage]
           self.layer.isHidden = true
       }
       
       func startAnimation() {
           if isAnimating {
               return
           }
           isAnimating = true
           self.layer.isHidden = false
           self.doAnimation()
           let delay = DispatchTime.now() + 10.0
           DispatchQueue.main.asyncAfter(deadline: delay) {
               if self.isAnimating {
                   print("菊花加载超时")
                    self.stopAnimation()
               }
           }
           
       }
       
       private func doAnimation() {
           imageView.animationDuration = 2    //设置动画总时间
           imageView.animationRepeatCount = 0   //设置重复次数,0表示不重复
           imageView .startAnimating()
       }
       
       func stopAnimation() {
           if isAnimating == false {
               return
           }
           
           isAnimating = false
           UIView.animate(withDuration: 0.3, animations: {
               self.alpha = 0
           }) { (finish) in
               self.imageView.stopAnimating()
               self.layer.isHidden = true
               self.alpha = 1
           }
       }
       
       required init?(coder aDecoder: NSCoder) {
           fatalError("init(coder:) has not been implemented")
       }
       
    }
    
    

    controller里代码如下

    import UIKit
    
    class LoadAnimationViewController: UIViewController {
        
        lazy var indicatorView:LoadAnimationView = {
            let view = LoadAnimationView.init(frame: CGRect(x: 0, y: self_NavigationH, width: kScreen_Width, height: kScreen_Height - self_NavigationH))
            self.view.addSubview(view)
            return view
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            self.indicatorView.startAnimation()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3.0) {
                self.indicatorView.stopAnimation()
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:ios 加载动画

          本文链接:https://www.haomeiwen.com/subject/kaoavqtx.html