Arn*_*ani 1 package swift swiftui
public struct CircularProgressView: View {
var count: Int
var total: Int
var progress: CGFloat
var fontOne: Font = Font.system(size: 75, weight: .bold, design: .rounded)
var fontTwo: Font = Font.system(size: 25, weight: .bold, design: .rounded)
var colorOne: Color = Color.primary
var colorTwo: Color = Color.gray
var fill = LinearGradient(gradient: Gradient(colors: [Color.green, Color.blue]), startPoint: .top, endPoint: .bottom)
var lineWidth: CGFloat = 25.0
public var body: some View {
ZStack{
Circle()
.stroke(lineWidth: lineWidth)
.opacity(0.3)
.foregroundColor(Color.secondary)
Circle()
.trim(from: 0.0, to: CGFloat(min(self.progress, 1.0)))
.stroke(fill ,style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
.rotationEffect(Angle(degrees: 270.0))
.animation(.linear, value: progress)
VStack {
Text("\(count)")
.font(fontOne)
.foregroundColor(colorOne)
Text("/ \(total)")
.font(fontTwo)
.foregroundColor(colorTwo)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,像 FontOne 和 fill 这样的变量是具有默认值的可选参数。
这是我试图在包中共享的视图的代码。我的测试用例成功了,我用这段代码测试了它
import XCTest
@testable import CircularProgress
import SwiftUI
final class CircularProgressTests: XCTestCase {
func testExample() {
let item = CircularProgressView(count: 5, total: 10, progress: 0.5)
XCTAssertEqual(item.progress, 0.5)
}
static var allTests = [
("testExample", testExample),
]
}
Run Code Online (Sandbox Code Playgroud)
我将该包添加到测试项目中,但是当我尝试执行以下操作时
import SwiftUI
import CircularProgress
struct ContentView: View {
var body: some View {
CircularProgressView(count: 5, total: 10, progress: 0.5)
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
我在 处收到错误CircularProgressView(count: 5, total: 10, progress: 0.5)。我收到的错误是“由于‘内部’保护级别,‘CircularProgressView’初始化器无法访问”
这是我的第一个包裹,我不确定我做错了什么。
注意:如果要导入包进行测试,请使用主分支,带有 1.0.0 标签的提交是旧版本。
在您的代码中,CircularProgressView您需要定义自己的初始化程序,因为编译器无法合成它。像这样的事情应该这样做:
public struct CircularProgressView: View {
var count: Int
var total: Int
var progress: CGFloat
// add the following to your CircularProgressView
public init (count: Int, total: Int, progress: CGFloat) {
self.count = count
self.total = total
self.progress = progress
}
// rest of code here
}
Run Code Online (Sandbox Code Playgroud)
如果您希望有其他可选参数,可以通过将初始化程序设置为可选参数的默认值来实现。可以通过以下方式完成:
public struct CircularProgressView: View {
var count: Int
var total: Int
var progress: CGFloat
var fontOne: Font
var fontTwo: Font
var colorOne: Color
var colorTwo: Color
var fill: LinearGradient
var lineWidth: CGFloat
public init(count: Int,
total: Int,
progress: CGFloat,
fontOne: Font = Font.system(size: 75, weight: .bold, design: .rounded),
fontTwo: Font = Font.system(size: 25, weight: .bold, design: .rounded),
colorOne: Color = Color.primary,
colorTwo: Color = Color.gray,
fill: LinearGradient = LinearGradient(gradient: Gradient(colors: [Color.green, Color.blue]), startPoint: .top, endPoint: .bottom),
lineWidth: CGFloat = 25.0) {
self.count = count
self.total = total
self.progress = progress
self.fontOne = fontOne
self.fontTwo = fontTwo
self.colorOne = colorOne
self.colorTwo = colorTwo
self.fill = fill
self.lineWidth = lineWidth
}
// remainder of code
}
Run Code Online (Sandbox Code Playgroud)
这基本上设置了所有默认值,因此您实际上不需要在结构中设置它们。
这也意味着您不需要第一个初始化程序,因为此初始化程序涵盖了这种情况。
然后你可以像这样使用它:
CircularProgressView(count: 5, total: 6, progress: 0.7)
CircularProgressView(count: 5, total: 6, progress: 0.7, colorTwo: Color.red, lineWidth: 6)
Run Code Online (Sandbox Code Playgroud)
只传入你想要使用的参数,但显然包括必需的参数count、total、 和progress。
| 归档时间: |
|
| 查看次数: |
3040 次 |
| 最近记录: |