根据WWDC视频梯度折线应该在SwiftUI map 视图中可用.

https://developer.apple.com/videos/play/wwdc2023/10043/?time=1360

let coordinates = [
        CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
        CLLocationCoordinate2D(latitude: 37.3352, longitude: -122.0322),
        CLLocationCoordinate2D(latitude: 34.0522, longitude: -118.2437)
    ]

    let gradient = LinearGradient(colors: [.red, .green, .blue], startPoint: .leading, endPoint: .trailing)

    let stroke = StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round, dash: [10, 10])

    var body: some View {
        Map {
            MapPolyline(coordinates: coordinates)
                .stroke(gradient, style: stroke)
            
        }
    }

似乎使用渐变的给定 colored颜色 中的第一种 colored颜色 来渲染整个多段线.这是一个错误吗/有没有其他方法来实现这一点?

推荐答案

你就差一点!如果你用一个简单的Gradient而不是LinearGradient来敲击键盘,它就可以工作了.

可以使用 colored颜色 数组或色标创建Gradient:

let gradient = Gradient(colors: [.red, .green, .blue])

// all other code unchanged

Screenshot

Swift相关问答推荐

如何将趋势线添加到线性图中?

防止NavigationLink自行返回导航视图

更改正在进行的异步任务的完成(SWIFT并发)(&q;)?

如何在每天午夜重置布尔值

并发访问SWIFT中数组的非重叠子范围

SwiftUI同心圆,通过逐渐出现来显示进度

可选,类似于自定义类型的初始化

try 从闭包访问返回的字符串时出现无法将类型 '()' 的返回表达式转换为返回类型 'String'编译器错误

字符串和整数格式的for循环,帮忙!

自定义碰撞形状出现在错误的位置

deinitialize() 与 deallocate()

如何在 Swift 中使用子定义覆盖父类扩展

使 Picker 与其他 BinaryInteger 类型兼容

如何以快速 Select 器菜单样式调整图像大小

调用从 SwiftUI 视图传递到 PageViewController.Coordinator 的函数

如何快速判断设备方向是横向还是横向?

如何删除桥头而不出错?

Swift 4 Decodable - 以枚举为键的字典

如何快速从另一个视图控制器重新加载表格视图

如何在swift中的每N个字符处为字符串添加分隔符?