ios 加载动画

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

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()
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 5,231评论 3 23
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,174评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,166评论 5 13
  • 拖延是大多数人都有的毛病,之所以迟迟不肯行动,心理因素远远大于事情本身的难度。今天,《1分钟能做什么?不可思议的“...
    来郑坤茹阅读 141评论 1 2