Swift中的UIBezierPath是什么?

In this article, we will create a pie chart using the UIBezierPath class that helps us create custom geometries.

在本文中,我们将使用UIBezierPath类创建一个饼图,该类可帮助我们创建自定义几何。

In just 100 lines of code, we will create our own UI element that you can reuse in your future applications.

仅用100行代码,我们将创建我们自己的UI元素,您可以在以后的应用程序中重用。

The source code of the project is available at the bottom of the article.

该项目的源代码位于文章底部。

开始吧 (Let’s Start)

First, let’s create a UIView subclass called PieChartView:

首先,让我们创建一个名为PieChartViewUIView子类:

We mark it as @IBDesignable to be able to easily debug it without needing to build and run the project. If you are not familiar with the declaration, feel free to quickly familiarize yourself with it in my article “What are @IBDesignable and @IBInspectable in Swift?

我们将其标记为@IBDesignable ,以便能够轻松调试它,而无需构建和运行项目。 如果您不熟悉该声明,请随时在我的文章“ Swift中的@IBDesignable和@IBInspectable是什么?

Our pie chart will have colored segments, so we need to create a property to manage them:

我们的饼图将带有彩色的细分,因此我们需要创建一个属性来对其进行管理:

Here we have the Segment struct conforming to Comparable. (We will want to figure out later which segment takes a higher percentage of the available space.) We also define the segments property as having a didSet property observer to trigger updates whenever the property changes.

在这里,我们的Segment结构符合Comparable 。 (我们稍后将要弄清楚哪个段占用了更多的可用空间。)我们还将segments属性定义为具有didSet属性观察器,以便在属性更改时触发更新。

Let’s now jump straight into the core of this tutorial: drawing the pie chart’s geometry:

现在,让我们直接进入本教程的核心:绘制饼图的几何:

We do two things here:

我们在这里做两件事:

  • Create the drawSegments(_ segments:) method that calculates the starting and ending angle of all segments and draws each one sequentially

    创建drawSegments(_ segments:)方法,该方法计算所有段的开始和结束角度,并依次绘制每个段

  • Override the draw(_ rect:) method and call the drawSegments(_ segments:) method

    重写draw(_ rect:)方法并调用drawSegments(_ segments:)方法

Let’s take a closer look at the drawSegments(_ segments:) method implementation:

让我们仔细看看drawSegments(_ segments:)方法的实现:

To draw a segment, we need the following things:

要绘制线段,我们需要满足以下条件:

  • Center of the pie chart

    饼图的中心
  • Radius of the pie chart

    饼图的半径
  • The starting angle (in radians) of the segment

    段的起始角度(弧度)
  • The ending angle (in radians) of the segment

    段的终止角度(以弧度为单位)

We can see how we easily obtain the center and the radius. The starting and ending angles are, however, more tricky.

我们可以看到如何轻松获得中心和半径。 但是,开始和结束角度比较棘手。

This is our logic for finding angles of each segment:

这是找到每个线段角度的逻辑:

  1. Start with 3 * .pi / 2 angle. This is going to be the beginning of the first segment.

    从3 * .pi / 2角开始。 这将是第一部分的开始。
  2. Iterate over segments.

    遍历段。
  3. Calculate the ending angle of each segment, taking into account the ratio of the segment.

    计算每个线段的终止角度,并考虑线段的比率。
  4. Draw the current segment using the obtained properties.

    使用获得的属性绘制当前线段。
  5. Set the startAngle property to the endAngle of the last-drawn segment.

    startAngle属性设置为最后绘制的线段的endAngle

  6. Repeat from step 3 until all segments are drawn.

    从第3步开始重复,直到绘制完所有线段。

It’s time to create the method that will create a UIBezierPath and draw a particular segment:

现在是时候创建将创建UIBezierPath并绘制特定段的方法了:

  • Create a path for our segment.

    为我们的细分创建路径。
  • Close the segment with a line.

    用一条线关闭线段。
  • Prepare the provided color to fill our segment.

    准备提供的color以填充我们的细分市场。

  • Fill the segment with the color.

    color填充细分。

The finishing step is to include that method inside the drawSegments(_ segments:) method. Now the final implementation looks like this:

完成步骤是将该方法包含在drawSegments(_ segments:)方法内。 现在,最终的实现如下所示:

The pie chart is done! We can now easily debug it using a .xib file:

饼图完成了! 现在,我们可以使用.xib文件轻松调试它:

Image for post

资源资源 (Resources)

Download the completed project on GitHub.

在GitHub上下载完成的项目

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/what-is-a-uibezierpath-in-swift-3e024af92e3d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值