快速动画:圆形虚线载入栏?

时间:2017-05-26 05:22:51

标签: ios animation swift3 uibezierpath cashapelayer

我已经在网上看了2到3个小时,遗憾的是找不到任何东西,(第二个问题:对swift不熟悉 - >这可以忽略不计)

  

基本上我需要圆形虚线加载条:每个圆点应该像圆形载荷一样单独动画..

我能够制作一个圆形的虚线圆圈,但无法为它制作动画......

任何帮助都会受到赞赏,即使这是来自github或其他任何东西的图书馆......

这是我创建圆形虚线圆圈的代码..

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100,y: 100), radius: 100.0, startAngle: 0.0, endAngle:CGFloat(M_PI * 2), clockwise: true)
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath
    shapeLayer.position = CGPoint(x: 100, y: 100)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.blue.cgColor
    shapeLayer.lineWidth = 6.0
    let one : NSNumber = 1
    let two : NSNumber = 13
    shapeLayer.lineDashPattern = [one,two]
    shapeLayer.lineCap = kCALineCapRound
    view.layer.addSublayer(shapeLayer)

}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()}
}

thats what I got

  

这就是我想要的(一岁以下):

enter image description here

2 个答案:

答案 0 :(得分:2)

我一直在玩自定义活动指标,所以这是一种方法

import UIKit

@IBDesignable class ActivityIndicatorCircle: UIView {
    var  timerInterval: Double   = 0.1
    var timer : Timer?
    var endAngle: CGFloat   = 0.0
    var angleStep           = CGFloat.pi / 20.0
    var angleOffset         = -CGFloat.pi / 2.0

    var shapeLayer = CAShapeLayer()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    func startAnimating() {
        superview?.bringSubview(toFront: self)

        layer.cornerRadius = frame.width / 2
        self.clipsToBounds = true

        isHidden = false
        timer?.invalidate()
        timer = nil

        timer = Timer.scheduledTimer(timeInterval: timerInterval,
                                 target: self,
                                 selector: #selector(self.updateCircle),
                                 userInfo: nil,
                                 repeats: true)
    }

    func stopAnimating() {
        isHidden = true
        timer?.invalidate()
        timer = nil
    }

    func updateCircle() {
        endAngle += angleStep
        if endAngle > CGFloat.pi * 2.0 {
            endAngle -= CGFloat.pi * 2.0
        }
        DispatchQueue.main.async {
            self.shapeLayer.removeFromSuperlayer() // remove the previous version

            let lineWidth: CGFloat = 6.0
            let radius = self.frame.size.width / 2.0 // if the view is square, this gives us center as well

            let circlePath = UIBezierPath(arcCenter: CGPoint(x: radius,y: radius), radius: radius - lineWidth, startAngle: self.angleOffset, endAngle: self.endAngle + self.angleOffset, clockwise: true)

            self.shapeLayer.path = circlePath.cgPath
            self.shapeLayer.position = CGPoint(x: 0, y: 0)
            self.shapeLayer.fillColor = UIColor.clear.cgColor
            self.shapeLayer.strokeColor = UIColor.blue.cgColor
            self.shapeLayer.lineWidth = lineWidth
            let one : NSNumber = 1
            let two : NSNumber = 13
            self.shapeLayer.lineDashPattern = [one,two]
            self.shapeLayer.lineCap = kCALineCapRound
            self.layer.addSublayer(self.shapeLayer)
        }
    }
}

要使用此功能,请在故事板中添加UIView,并将课程设置为ActivityIndicatorCircle

要启动它,请致电activityIndicatorCircle.startAnimating()

答案 1 :(得分:0)

对于循环加载器,您可以使用此库来帮助您。 https://github.com/naoyashiga/RPLoadingAnimation