1、思路:
新建一个view,添加shape,给予一个动画实现。
2、效果图:
效果1:
效果2:
gif有点卡,代码运行不会这样。
3、源码(整个类放进来的)
效果1源码:
// // YJDownloadingCircle.swift // k12_sl_iOS // // Created by liyajun on 2017/7/13. // // import UIKit class YJDownloadingCircle: UIView { var loadingLayer:CAShapeLayer! = nil override init(frame: CGRect) { super.init(frame: frame) initViews() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initViews() } override func awakeFromNib() { initViews() } func initViews() { backgroundColor = UIColor.white } func drawHalfCircle() { loadingLayer = self.drawCircle() loadingLayer.strokeStart = 0.0 loadingLayer.strokeEnd = 0.75 let basicAni = CABasicAnimation(keyPath: "transform.rotation.z") basicAni.fromValue = 0.0 basicAni.toValue = M_PI*2 basicAni.duration = 0.5 basicAni.repeatCount = MAXFLOAT basicAni.autoreverses = false basicAni.fillMode = kCAFillModeForwards self.layer.add(basicAni, forKey: nil) } private func drawCircle() -> CAShapeLayer { let circleLayer = CAShapeLayer() let rect = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height) circleLayer.frame = rect circleLayer.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2) circleLayer.fillColor = UIColor.clear.cgColor circleLayer.lineWidth = 1 circleLayer.strokeColor = UIColor.colorWithHex(hex: "FF3B30").cgColor let bezier = UIBezierPath(ovalIn: rect) circleLayer.path = bezier.cgPath self.layer.addSublayer(circleLayer) return circleLayer } }