gpt4 book ai didi

ios - 如何给 SwiftUI arc 添加阴影?

转载 作者:行者123 更新时间:2023-12-01 15:41:23 25 4
gpt4 key购买 nike

我有以下弧线:

struct Arc : Shape
{
@Binding var endAngle: Double
var center: CGPoint
var radius: CGFloat
func path(in rect: CGRect) -> Path
{
var path = Path()

path.addArc(center: center, radius: radius, startAngle: .degrees(270), endAngle: .degrees(endAngle), clockwise: false)

return path.strokedPath(.init(lineWidth: 50, lineCap: .round))
}
}

我如何添加阴影,类似于 Apple Watch 上的事件弧线,以便在完整的圆圈中 endAngle 仍然清晰可见?

编辑:在 360 度以上(如此完整的圆)还有一个额外的问题,即两个圆弧端合并并显示为一条径向线(我看到这个是因为我应用了一个 AngularGradient)。当然 Arc 不会像 Apple Watch arcs 那样做高级操作,比如在 startAngle 位置上方继续。但这就是我要找的。有谁知道如何在 SwiftUI 中做到这一点?

最佳答案

enter image description here

这个呢?好的......有点“棘手”

import SwiftUI

struct Arc: Shape {

@Binding var startAngle: Double
@Binding var endAngle: Double

var center: CGPoint
var radius: CGFloat
var color: Color

func path(in rect: CGRect) -> Path {
var path = Path()

let cgPath = CGMutablePath()
cgPath.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true)

// path.addArc(center: center, radius: radius, startAngle: .degrees(270), endAngle: .degrees(endAngle), clockwise: false)

path = Path(cgPath)

return path.strokedPath(.init(lineWidth: 50, lineCap: .round))
}
}

struct ContentView: View {
var body: some View {
ZStack() {
// Arc(endAngle: .constant(269), center: CGPoint(x: 200, y: 200), radius: 150, color: .red).foregroundColor(.red).opacity(0.2)
Arc(startAngle: .constant(80 + 271), endAngle: .constant(80 + 271 + 340), center: CGPoint(x: 205, y: 205), radius: 150, color: .red).foregroundColor(.red)//.shadow(color: .black, radius: 5, x: -30, y: -170)
Arc(startAngle: .constant(90), endAngle: .constant(80 + 270), center: CGPoint(x: 200, y: 200), radius: 150, color: .red).foregroundColor(.red).shadow(color: .black, radius: 5, x: -114, y: -230)
}
.background(Color.black)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

关于ios - 如何给 SwiftUI arc 添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58777889/

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