1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54//
// RingView.swift
// DesignCode
//
// Created by EDZ on 2020/5/4.
// Copyright © 2020 ykq. All rights reserved.
//
import SwiftUI
struct RingView: View {
var color1 = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
var color2 = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
var width : CGFloat = 88
var height : CGFloat = 88
var percent : CGFloat = 44
@Binding var show : Bool
var body: some View {
let multiplier = width/44
let progress = 1 - percent / 100
return ZStack {
//背景circle
Circle()
.stroke(Color.black.opacity(0.1),style: StrokeStyle(lineWidth: 5 * multiplier ))
.frame(width: width, height: height)
// 百分比circle
Circle()
.trim(from: show ? progress : 1, to: 1) // 裁剪圆环
.stroke(LinearGradient(gradient: Gradient(colors: [Color(color1),Color(color2)]), startPoint: .topTrailing, endPoint: .bottomLeading),style:StrokeStyle(lineWidth: 5 * multiplier, lineCap: .round, lineJoin: .round, miterLimit: .infinity, dash: [20,0], dashPhase: 0) )
.rotationEffect(Angle(degrees: 90))
.rotation3DEffect(Angle(degrees: 180), axis: (x: 1, y: 0, z: 0)) // 绕x旋转180
.frame(width: width, height: height)
.shadow(color: Color(color2).opacity(0.1), radius: 3 * multiplier, x: 0, y: 3 * multiplier)
//.animation(.easeInOut) // 动画在父级用,子级的动画会覆盖父级
Text("\(Int(percent))%").font(.system(size: 14 * multiplier)).fontWeight(.bold)
.onTapGesture {
self.show.toggle()
}
}
}
}
struct RingView_Previews: PreviewProvider {
static var previews: some View {
RingView(show: .constant(true))
}
}