动画UIBezierPath动画iOS

UIBezierPath 学习笔记

2016-03-03  本文已影响2408人  raydang

简介:

        贝塞尔曲线,是应用于二维图形应用程序的数学曲线。可以创建基于矢量的路径。这个类在UIKit中。此类是Core Graphics框架对CGContextRef的一个封装。使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状。曲线定义:起始点、终止点(锚点)、控制点。通过调整控制点,贝塞尔曲线的形状会发生变化。

使用UIBezierPath画图步骤:

1、创建一个UIBezierPath对象;

2、调用-moveToPoint:设置初始线段的起点;

3、添加线或者曲线去定义一个或者多个子路径;

4、改变UIBezierPath对象跟绘图相关的属性。如,我们可以设置画笔的属性、填充样式等。

创建方法:

+ (instancetype)bezierPath;使用比较多,因为这方法创建的对象,我们可以根据我们的需要任意定制样式,可以画任何我们想画的图形。

+ (instancetype)bezierPathWithRect:(CGRect)rect;这方法根据一个矩形画贝塞尔曲线。

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;这方法根据一个矩形画内切曲线。通常用它来画圆或者椭圆。

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;画矩形,但是这个矩形是可以画圆角的。第一个参数是矩形,第二个参数是圆角大小。

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii; 画矩形,但是可以指定某一个角画成圆角。

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;用于画圆弧,参数说明如下: center: 弧线中心点的坐标 radius: 弧线所在圆的半径 startAngle: 弧线开始的角度值 endAngle: 弧线结束的角度值 clockwise: 是否顺时针画弧线。

+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;这个方法创建并返回一个 Core Graphics path。

- (UIBezierPath *)bezierPathByReversingPath;创建并返回一个新的反转内容的当前路径曲线路径的对象。

构建一个path:

- (void)moveToPoint:(CGPoint)point; 设置第一个起始点到接收器

- (void)addLineToPoint:(CGPoint)point;附加一条直线到接收器的路径

- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2; 该方法就是画三次贝塞尔曲线的关键方法,以三个点画一段曲线,一般和moveToPoint:配合使用。其实端点为moveToPoint:设置,终止端点位为endPoint;。控制点1的坐标controlPoint1,这个参数可以调整。控制点2的坐标是controlPoint2。

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;画二次贝塞尔曲线,是通过调用此方法来实现的。一般和moveToPoint:配合使用。endPoint终端点,controlPoint控制点,对于二次贝塞尔曲线,只有一个控制点。

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise ;

- (void)closePath;闭合线

- (void)removeAllPoints;移除所有的点

- (void)appendPath:(UIBezierPath *)bezierPath;追加指定的路径对象到内容到接收器的路径

- (void)applyTransform:(CGAffineTransform)transform;用指定的仿射变换矩阵变换路径的所有点

path 信息:

@property(readonly,getter=isEmpty) BOOL empty;该值指示路径是否有任何有效的元素。

@property(nonatomic,readonly) CGRect bounds;路径包括的矩形。

@property(nonatomic,readonly) CGPoint currentPoint;图形路径中的当前点。

- (BOOL)containsPoint:(CGPoint)point;接收器是否包含指定的点。

绘制属性:

@property(nonatomic) CGFloat lineWidth; 线宽。

@property(nonatomic) CGLineCap lineCapStyle; 端点类型。

typedef CF_ENUM(int32_t, CGLineCap) {

    kCGLineCapButt,   //默认的

    kCGLineCapRound,//轻微圆角

    kCGLineCapSquare //正方形

};

@property(nonatomic) CGLineJoin lineJoinStyle; 连接类型。

typedef CF_ENUM(int32_t, CGLineJoin) {

    kCGLineJoinMiter, //默认的表示斜接

    kCGLineJoinRound,//圆滑衔接

    kCGLineJoinBevel   //斜角连接

};

@property(nonatomic) CGFloat miterLimit; 这个限制值有助于避免在连接线段志坚的尖峰

@property(nonatomic) CGFloat flatness;确定弯曲路径短的绘制精度的因素

@property(nonatomic) BOOL usesEvenOddFillRule; 判断奇偶数组的规则绘制图像路径

- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase; 设置线型

- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase; 检索线型

当前图形上下文中的路径操作:

- (void)fill;  填充颜色

- (void)stroke; 各个点连线

- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;填充

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

- (void)addClip;与封闭的接收器的路径与当前徒刑上下文的裁剪路径的面积

上一篇下一篇

猜你喜欢

热点阅读