gpt4 book ai didi

ios - 如何从自定义 UIView 类调用函数?

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:56 24 4
gpt4 key购买 nike

我的第一个 View Controller 中有 UIView。我已使用以下代码将该 UIView 类设置为自定义进度 View 。

class ProgressView: UIView {
let trackLayer = CAShapeLayer()
let shapeLayer = CAShapeLayer()

static let instance = ProgressView()

override func awakeFromNib() {
super.awakeFromNib()
let center = CGPoint.init(x: frame.width / 2, y: frame.height / 2)
let circularPath = UIBezierPath(arcCenter: center, radius: 30, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)

trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
trackLayer.lineWidth = 3
trackLayer.fillColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
trackLayer.lineCap = kCALineCapRound
self.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
shapeLayer.lineWidth = 3
shapeLayer.fillColor = #colorLiteral(red: 0.3794638515, green: 0.5145698786, blue: 0.9605538249, alpha: 1)
shapeLayer.lineCap = kCALineCapRound
shapeLayer.strokeEnd = 0
self.layer.addSublayer(shapeLayer)
}
}

自定义 UIView 类中还有一个函数,用于启动填充圆圈的动画,代码如下

 func startProgress() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 60
basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "soBasic")
}

我需要在我的 First View Controller 中调用 startProgress 函数。在 viewDidLoad 中,我调用了 ProgressView.instance.startProgress() 但它没有触发。如何从另一个类调用开始进度函数?

最佳答案

ProgressView 中你需要创建一个初始化或者你可以使用默认的。

class ProgressView: UIView {

//you can set any additional properties in it.

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

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

如何使用

在你想使用的 View Controller 中...

let pgView = ProgressView() //or any other initialise if you have created
.
.
.
pgView.startProgress()

关于ios - 如何从自定义 UIView 类调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50713728/

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