在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文:https://www.hackingwithswift.com/books/ios-swiftui/creating-custom-paths-with-swiftui SwiftUI gives us a dedicated Just like colors, gradients, and shapes, paths are views in their own right. This means we can use them just like text views and images, although as you’ll see it’s a bit clumsy. Let’s start with a simple shape: drawing a triangle. There are a few ways of creating paths, including one that accepts a closure of drawing instructions. This closure must accept a single parameter, which is the path to draw into. I realize this can be a bit brain-bending at first, because we’re creating a path and inside the initializer for the path we’re getting passed the path to draw into, but think of it like this: SwiftUI is creating an empty path for us, then giving us the chance to add to it as much as we want. Paths have lots of methods for creating shapes with squares, circles, arcs, and lines. For our triangle we need to move to a stating position, then add three lines like this: var body: some View { Path { path in path.move(to: CGPoint(x: 200, y: 100)) path.addLine(to: CGPoint(x: 100, y: 300)) path.addLine(to: CGPoint(x: 300, y: 300)) path.addLine(to: CGPoint(x: 200, y: 100)) } }
We haven’t used When our triangle code runs, you’ll see a large black triangle. Where you see it relative to your screen depends on what simulator you are using, which is part of the problem of these raw paths: we need to use exact coordinates, so if you want to use a path by itself you either need to accept that sizing across all devices or use something like We’ll look at a better option shortly, but first let’s look at coloring our path. One option is to use the Path { path in path.move(to: CGPoint(x: 200, y: 100)) path.addLine(to: CGPoint(x: 100, y: 300)) path.addLine(to: CGPoint(x: 300, y: 300)) path.addLine(to: CGPoint(x: 200, y: 100)) } .fill(Color.blue) We can also use the .stroke(Color.blue, lineWidth: 10)
That doesn’t look quite right, though – the bottom corners of our triangle are nice and sharp, but the top corner is broken. This happens because SwiftUI makes sure lines connect up neatly with what comes before and after rather than just being a series of individual lines, but our last line has nothing after it so there’s no way to make a connection. One way to fix this is just to draw the first line again, which means the last line has a connecting line to match up with: Path { path in path.move(to: CGPoint(x: 200, y: 100)) path.addLine(to: CGPoint(x: 100, y: 300)) path.addLine(to: CGPoint(x: 300, y: 300)) path.addLine(to: CGPoint(x: 200, y: 100)) path.addLine(to: CGPoint(x: 100, y: 300)) } .stroke(Color.blue, lineWidth: 10)
That works great, as you can see. And it even works great with transparency: if use a transparent stroke color such as An alternative is to use SwiftUI’s dedicated .stroke(Color.blue, style: StrokeStyle(lineWidth: 10, lineCap: .round, lineJoin: .round))
With that in place you can remove the extra line from our path, because it’s no longer needed. Using rounded corners solves the problem of our rough edges, but it doesn’t solve the problem of fixed coordinates. For that we need to move on from paths and look at something more complex: shapes. |
请发表评论