ios - swift 中的循环进度

标签 ios swift uiview progress-bar

我想在 swift 中制作一个简单的循环进度条。到目前为止我没能做到的是,进度应该从圆的顶部开始(目前它是从 90 度点开始的)。我还想在进度线下方有一条圆形线,它应该比进度线更粗并且颜色也不同。

任何人都可以让我朝着正确的方向前进吗?

这是我的功能:

func animateView() {

let circle = viewProgress // viewProgress is a UIView

        var progressCircle = CAShapeLayer()

        let circlePath = UIBezierPath(ovalInRect: circle.bounds)

        progressCircle = CAShapeLayer ()
        progressCircle.path = circlePath.CGPath
        progressCircle.strokeColor = UIColor.greenColor().CGColor
        progressCircle.fillColor = UIColor.clearColor().CGColor
        progressCircle.lineWidth = 5.0

        circle.layer.addSublayer(progressCircle)

        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1.5
        animation.duration = 1
        animation.fillMode = kCAFillModeForwards
        animation.removedOnCompletion = false

        progressCircle.addAnimation(animation, forKey: "ani")
    }

Igor 这是它的样子:

enter image description here

最佳答案

对于 swift 4

声明两个 CAShapeLayers。一个用于实际循环,另一个用于进度层!

func createCircularPath() {

let circleLayer = CAShapeLayer()
let progressLayer = CAShapeLayer()

let center = self.view.center
let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -.pi / 2, endAngle: 2 * .pi, clockwise: true)

circleLayer.path = circularPath.cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.strokeColor = UIColor.lightGray.cgColor
circleLayer.lineCap = .round
circleLayer.lineWidth = 20.0  //for thicker circle compared to progressLayer

progressLayer.path = circularPath.cgPath
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.strokeColor = UIColor.blue.cgColor
progressLayer.lineCap = .round
progressLayer.lineWidth = 10.0
progressLayer.strokeEnd = 0

view.addSublayer(circleLayer)
view.addSublayer(progressLayer)

let progressAnimation = CABasicAnimation(keyPath: "strokeEnd")
progressAnimation.toValue = 1
progressAnimation.fillMode = .forwards
progressAnimation.isRemovedOnCompletion = false

progressLayer.add(progressAnimation, forKey: "progressAnim") 

}

编码愉快!

希望这个答案对您有所帮助:]

关于ios - swift 中的循环进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40176481/

相关文章:

ios - Swift:错误: "required' 初始化程序 'init(coder:)' 必须由 'NSURLRequest' 的子类提供”

ios - iOS 模拟器的 GPX 文件

ios - 如何将 Firebase 下载文件与进度 View 链接?

swift - 加载单独的 WKWebViews 时单独的 cookies?

ios - 通过标签动态查找 iOS UIView

ios - 子类化包含多个 UIView 的 UIView

ios - 按下按钮时 KLCPopup 错误

ios - 如何以编程方式将支付卡添加到苹果钱包中

ios - UIBarButtonItem 样式被忽略?

ios - CoreData写对象慢