【发布时间】:2019-11-09 04:30:39
【问题描述】:
在 UIKit 中绘制描边和填充的路径/形状非常容易。
例如,下面的代码绘制了一个蓝色的红色圆圈。
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
let center = CGPoint(x: rect.midX, y: rect.midY)
ctx.setFillColor(UIColor.red.cgColor)
ctx.setStrokeColor(UIColor.blue.cgColor)
let arc = UIBezierPath(arcCenter: center, radius: rect.width/2, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
arc.stroke()
arc.fill()
}
如何使用 SwiftUI 做到这一点?
Swift UI 似乎支持:
Circle().stroke(Color.blue)
// and/or
Circle().fill(Color.red)
但不是
Circle().fill(Color.red).stroke(Color.blue) // Value of type 'ShapeView<StrokedShape<Circle>, Color>' has no member 'fill'
// or
Circle().stroke(Color.blue).fill(Color.red) // Value of type 'ShapeView<Circle, Color>' has no member 'stroke'
我应该只 ZStack 两个圆圈吗?这似乎有点傻。
【问题讨论】: