引导
记得刚接触 iOS 的时候, 第一次看到绘图和动画做出来的效果时,只有一个感觉酷!高大上!(默默下决心我要学会☺).
在写出这些牛X效果的前提 贝塞尔曲线
这个东西我觉得要做到初步理解,然后梳理总结,到最后夯实基础、活学活用。
正题
在这里首先附上官方原文地址 UIBezierPath Class & UIBezierPath.h 属性和方法
Relationships | |
---|---|
Inherits From NSObject | 继承自: NSObject |
Conforms To NSCoding, NSCopying | 遵守: NSCoding,NSObject |
Framework: UIKit in iOS 3_2 | iOS 3.2+ |
Class
UIBezierPath
The
UIBezierPath
class lets you define a path consisting of straight and curved line segments and render that path in your custom views.
You use this class initially to specify just the geometry for your path.
Paths can define simple shapes such as rectangles, ovals, and arcs or they can define complex polygons that incorporate a mixture of straight and curved line segments.
After defining the shape, you can use additional methods of this class to render the path in the current drawing context.
UIBezierPath
类允许你在自定义的 View
中绘制和渲染由直线和曲线组成的路径. 你可以在初始化的时候, 直接为你的 UIBezierPath
指定一个几何路径. 路径可以是简单的几何图形例如: 矩形、椭圆、弧线之类的, 也可以是相对比较复杂的由直线和曲线组成的多边形. 当你定义完图形以后, 你可以使用这个类的其他的方法将你的路径直接绘制在当前的绘图上下文中.
Overview
A
UIBezierPath
object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. Once you have the object configured the way you want it, you can tell it to draw itself in the current context. Because the creation, configuration, and rendering process are all distinct steps, Bezier path objects can be reused easily in your code. You can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls.
UIBezierPath
是由几何路径 和 属性描述组成的, 属性是用来在渲染阶段描绘几何路径的, 比如线宽之类的东西. 路径和属性是完全独立的, 他们并不互相依赖, 你可以分开分开去设置他们. 一旦你以自己喜欢的方式配置了 UIBezierPath
对象, 你就可以调用方法通知UIBezierPath
在当前的绘图上下文中绘制图形了. 因为创建、 配置、 渲染路径等操作, 都是完全不同的步骤, 所以你可以在你的代码中非常容易的对UIBezierPath
对象进行复用. 你甚至可以使用同一个 UIBezierPath
对象去渲染同一个图形很多次, 你也可以再多次渲染的间隔中, 修改属性来渲染出不同样式的路径.
You set the geometry of a path by manipulating the path’s current point. When you create a new empty path object, the current point is undefined and must be set explicitly. To move the current point without drawing a segment, you use the
moveToPoint:
method. All other methods result in the addition of either a line or curve segments to the path. The methods for adding new segments always assume you are starting at the current point and ending at some new point that you specify. After adding the segment, the end point of the new segment automatically becomes the current point.
你可以通过控制路径的当前的(起始)点来设置一条路径的形状,当你创建了一个空的UIBezierPath 对象时, 起始点currentPoint
这个属性是未定义的, 你需要手动的去设置.currentPoint. 如果你希望在不绘制任何线条的情况下移动currentPoint, 你可以使用 moveToPoint:方法. 其他的方法都会导致在你的路径中添加额外的直线或曲线. 所有构造路径相关的方法, 都会以当前路径的currentPoint 为起点, 以你指定的endPoint为终点进行绘制. 当完成绘制之后, 会自动将新增的这条线的终点设置为UIBezierPath 对象的currentPoint.
A single Bezier path object can contain any number of open or closed subpaths, where each subpath represents a connected series of path segments. Calling the
closePath
method closes a subpath by adding a straight line segment from the current point to the first point in the subpath. Calling themoveToPoint:
method ends the current subpath (without closing it) and sets the starting point of the next subpath. The subpaths of a Bezier path object share the same drawing attributes and must be manipulated as a group. To draw subpaths with different attributes, you must put each subpath in its ownUIBezierPath
object.
一个单一的贝塞尔路径对象能够包含任意数量的开放和闭合的子路径,在这些路径的线段中,每一个线段都是串联的. 调用closePath
方法将会闭合路径, 它将会从currentPoint
到 子路经的 firstPoint起点
绘制一条直线. 调用moveToPoint:
方法将会结束当前的子路径, 但是并不会自动闭合当前的自路径, 并且会将currentPoint
移动到指定的点, 也就是下一条绘制路径的起始点.UIBezierPath
中所有的自路径都会共享同样的绘图属性. 如果你希望绘制一些子路径, 但是不适用相同的绘图属性, 那么你就只能创建很多的UIBezierPath 对象来承载每一条路径.
After configuring the geometry and attributes of a Bezier path, you draw the path in the current graphics context using the
stroke
andfill
methods. The stroke method traces the outline of the path using the current stroke color and the attributes of the Bezier path object. Similarly, the fill method fills in the area enclosed by the path using the current fill color. (You set the stroke and fill color using theUIColor
class.)
当你为UIBezierPath
对象配置完几何路径和绘图属性之后, 你就可以使用stroke
描边(空心) 和 fill
填充(实心) 方法在当前的绘图上下文中进行绘制了. stroke
方法将会使用当前的strokeColor
和 绘图属性来描绘曲线的轮廓. 同样的, fill
方法将会使用fillColor
来填充路径所围成的图形(使用UIColor
类方法来设置strokeColor
和 fillColor
).
In addition to using a Bezier path object to draw shapes, you can also use it to define a new clipping region. The
addClip
method intersects the shape represented by the path object with the current clipping region of the graphics context. During subsequent drawing, only content that lies within the new intersection region is actually rendered to the graphics context.
除了可以用贝塞尔路径对象去绘制图形,你还可以利用它去定义个新的裁剪区域.addClip
方法通过当前图形上下文裁剪的区域的路径对象来表示两个图形的相交. 在随后的绘制过程中,只有处在新的交集区域内的内容实际上是被图形上下文绘制的
然后我们就直接从UIBezierPath.h
入手吧
UIBezierPath.h
创建 UIBezierPath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
构造路径
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
绘图属性
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
绘制路径
|
|
|
|
|
|
|
|
剪切路径
|
|
Hit Detection
|
|
|
|
|
|
|
|
|
|
实战示例
UIBezierPath.h
文件大家完全的过了一遍了, 其实看上去还是蛮简单过的昂.. 接下来就到了激动人心的实战环节了. 我们来简单的写几句代码, 看看效果吧.
1、bezierPathByReversingPath:路径反转
效果:
2、setLineDash: 虚线
效果:
要想做出多牛逼酷炫的动画效果,就必须承受同等学习的代价 !
期待
如果在阅读过程中遇到 error || new ideas,希望你能 messages 我,我会及时改正谢谢。
点击右上角的 喜欢 和 订阅Rss 按钮,可以收藏本仓库,并在 Demo 更新时收到邮件通知。