UIBezierPath类详细解析(一) —— 基本概览

版本记录

版本号 时间
V1.0 2018.01.18

前言

在iOS中不管是画图或者动画,都需要指名路径,我们经常用的就是UIBezierPath贝塞尔路径,接下来这几篇我们就详细的介绍下这个类和基本用法。

API

下面我们就先看一下UIBezierPath类的API。

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIBezierPath : NSObject<NSCopying, NSSecureCoding>

+ (instancetype)bezierPath;
+ (instancetype)bezierPathWithRect:(CGRect)rect;
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
+ (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;
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// Returns an immutable CGPathRef which is only valid until the UIBezierPath is further mutated.
// Setting the path will create an immutable copy of the provided CGPathRef, so any further mutations on a provided CGMutablePathRef will be ignored.
@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;

// Path construction

- (void)moveToPoint:(CGPoint)point;
- (void)addLineToPoint:(CGPoint)point;
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
- (void)closePath;

- (void)removeAllPoints;

// Appending paths

- (void)appendPath:(UIBezierPath *)bezierPath;

// Modified paths

- (UIBezierPath *)bezierPathByReversingPath NS_AVAILABLE_IOS(6_0);

// Transforming paths

- (void)applyTransform:(CGAffineTransform)transform;

// Path info

@property(readonly,getter=isEmpty) BOOL empty;
@property(nonatomic,readonly) CGRect bounds;
@property(nonatomic,readonly) CGPoint currentPoint;
- (BOOL)containsPoint:(CGPoint)point;

// Drawing properties

@property(nonatomic) CGFloat lineWidth;
@property(nonatomic) CGLineCap lineCapStyle;
@property(nonatomic) CGLineJoin lineJoinStyle;
@property(nonatomic) CGFloat miterLimit; // Used when lineJoinStyle is kCGLineJoinMiter
@property(nonatomic) CGFloat flatness;
@property(nonatomic) BOOL usesEvenOddFillRule; // Default is NO. When YES, the even-odd fill rule is used for drawing, clipping, and hit testing.

- (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;

// Path operations on the current graphics context

- (void)fill;
- (void)stroke;

// These methods do not affect the blend mode or alpha of the current graphics context
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

- (void)addClip;

@end

NS_ASSUME_NONNULL_END

Overview

由可以在自定义视图中呈现的直线段和曲线段组成的路径。

您最初使用这个类来指定您的路径的几何。 路径可以定义简单的形状,如矩形,椭圆形和圆弧,或者可以定义包含直线和曲线线段混合的复杂多边形。 定义形状之后,可以使用此类的其他方法在当前绘图上下文中渲染路径。

UIBezierPath对象将路径的几何与在渲染期间描述路径的属性相结合。 您可以分别设置几何和属性,并可以相互独立地进行更改。 按照你想要的方式配置对象之后,你可以告诉它在当前的上下文中绘制自己。 因为创建,配置和渲染过程都是不同的步骤,所以Bézier路径对象可以在代码中轻松重用。 甚至可以使用相同的对象多次渲染相同的形状,也许可以在连续的绘制调用之间更改渲染选项。

您可以通过操作路径的当前点来设置路径的几何图形。当您创建一个新的空路径对象时,当前点是未定义的,必须显式设置。要移动当前点而不绘制线段,请使用moveToPoint:方法。所有其他方法都会将路线或曲线段添加到路径中。添加新线段的方法总是假设您从当前点开始,到您指定的新点为止。添加分段后,新分段的终点将自动成为当前点。

单个Bézier路径对象可以包含任意数量的打开或关闭的子路径,其中每个子路径代表连接的一系列路径段。调用closePath方法通过添加从当前点到子路径中第一个点的直线段来关闭子路径。调用moveToPoint:方法结束当前子路径(不关闭它)并设置下一个子路径的起始点。 Bézier路径对象的子路径共享相同的图形属性,并且必须作为一个组进行操作。要绘制具有不同属性的子路径,您必须将每个子路径放在它自己的UIBezierPath对象中。

配置Bézier路径的几何和属性之后,可以使用stroke和fill方法在当前图形上下文中绘制路径。 stroke方法使用当前描边颜色和Bézier路径对象的属性来追踪路径的轮廓。 同样,填充方法使用当前填充颜色填充路径所包围的区域。 (您可以使用UIColor类设置 stroke和 fill颜色。)

除了使用Bézier路径对象绘制形状之外,还可以使用它来定义新的裁剪区域。 addClip方法将由路径对象表示的形状与图形上下文的当前剪切区域相交。 在随后的绘制过程中,只有位于新交集区域内的内容实际呈现给图形上下文。


Topics

1. Creating a UIBezierPath Object

2. Constructing a Path

3. Accessing Drawing Properties

4. Drawing Paths

  • - fill

    • 使用当前绘图属性绘制由接收者路径包围的区域。
  • - fillWithBlendMode:alpha:

    • 使用指定的混合模式和透明度值绘制接收者路径所包围的区域。
  • - stroke

    • 使用当前绘图属性沿接收器的路径绘制一条线。
  • - strokeWithBlendMode:alpha:

    • 使用指定的混合模式和透明度值沿着接收者的路径绘制一条线。

5. Clipping Paths

  • - addClip
    • 将接收器路径所包围的区域与当前图形上下文的剪切路径相交,并将生成的形状作为当前剪切路径。

6. Hit Detection

  • - containsPoint:

    • 返回一个布尔值,指示接收方包含的区域是否包含指定的点。
  • empty

    • 指示路径是否有任何有效元素的布尔值。
  • bounds

    • 路径的边界矩形。

7. Applying Transformations

8. Constants

9. Conforms To

后记

这篇结束,下一篇主要就是根据代码详细的介绍这个类的用法。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容