gpt4 book ai didi

ios - swift 中的动画 : Circular Dashed Loading Bar?

转载 作者:行者123 更新时间:2023-11-28 21:09:25 24 4
gpt4 key购买 nike

我已经在网上浏览了大约 2 到 3 个小时,不幸的是找不到任何东西,(第二个问题:刚接触 swift -> 这个可以忽略)

Basically I need Circular dashed Loading bar : Every dot should animate Individually like a circular load..

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

任何帮助将不胜感激,即使那是来自 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

This is what I want (Below One):

enter image description here

最佳答案

我一直在研究自定义事件指示器,所以这里有一种方法可以做到这一点

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 添加到 Storyboard,并将类设置为 ActivityIndi​​catorCircle

要启动它,请调用 activityIndi​​catorCircle.startAnimating()

关于ios - swift 中的动画 : Circular Dashed Loading Bar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com