热点新闻
iOS动画 —— UIBezierPath
2023-07-07 22:05  浏览:691  搜索引擎搜索“手机速企网”
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在手机速企网看到的信息,谢谢。
展会发布 展会网站大全 报名观展合作 软文发布

简介

UIBezierPath类创建基于矢量的路径,例如椭圆或者矩形,或者有多个直线和曲线段组成的形状。

UIBezierPath是UIKit中的一个关于图形绘制的类,是通过Quartz 2D也就是CG(Core Graphics)CGPathRef的封装得到的,从高级特性支持来看不及CG。

使用UIBezierPath,你只能在当前图形上下文中绘制。 CGContextRef即图形上下文。
1.重写UIView的drawRect方法,在该方法里便可得到context;
2.调用UIGraphicsBeginImageContextWithOptions方法得到

drawRect: 触发触发时机
1、当view第一次显示到屏幕上时;
2、当调用view的setNeedsDisplay或者setNeedsDisplayInRect:方法时。

绘制流程

  1. 初始化一个 UIBezierPath 对象
  2. 设置相关的属性;
  3. 调用 -moveToPoint: 方法初始线段的起点;
  4. 添加线段或者曲线段去构建一个或者多个子路径;

- (void)drawRect:(CGRect)rect // 重写drawRect方法 { // 1.初始化图形相应的UIBezierPath对象 UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 200)]; // 2. // 2.设置一些修饰属性 aPath.lineWidth = 8.0; //路径的终点形状, aPath.lineCapStyle = kCGLineCapRound; //路径的连接点形状 aPath.lineJoinStyle = kCGLineCapRound; UIColor *color = [UIColor colorWithRed:0 green:0 blue:0.7 alpha:1]; [color set]; // 3.起点 [aPath moveToPoint:CGPointMake(20, 20)]; // 4.绘制线条 [aPath addLineToPoint:CGPointMake(100, 100)]; [aPath stroke]; // 渲染,完成绘制 }

创建 UIBezierPath

  • 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype) bezierPath;

  • 通过一个矩形, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype)bezierPathWithRect:(CGRect)rect;

  • 通过一个指定的矩形中的椭圆形, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

  • 根据一个圆角矩形, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype) bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

  • 根据一个圆角矩形, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype) bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

  • 通过一个圆弧, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype) bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;




  • 通过一个 CGPath, 创建并且返回一个新的 UIBezierPath 对象

+ (instancetype) bezierPathWithCGPath:(CGPathRef)CGPath;

  • 创建并返回一个新的BezierPath, 这个 BezierPath 的方向是原 BezierPath 的反方向

- (UIBezierPath *) bezierPathByReversingPath;

构造路径

  • UIBezierPath 对象的 currentPoint 移动到指定的点

- (void)moveToPoint:(CGPoint)point;

  • 在当前子路径中追加一条直线

- (void)addLineToPoint:(CGPoint)point;

  • 在当前子路径中追加一条圆弧

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

  • 在当前 子路经中追加一条 二次贝塞尔曲线

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;




  • 在当前 子路经中追加一条 三次贝塞尔曲线

- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;




  • 关闭当前子路经

- (void)closePath;

  • 删除 UIBezierPath 对象中的所有点, 效果也就等同于删除了所有子路经

- (void)removeAllPoints;

  • 将指定 UIBezierPath 中的内容添加到当前 UIBezierPath 对象中

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

  • UIBezierPath 中的 CGPath 对象

@property(nonatomic) CGPathRef CGPath;

  • 绘图路径中的当前点

@property(nonatomic, readonly) CGPoint currentPoint;

绘图属性

  • 线宽

@property(nonatomic) CGFloat lineWidth;

  • 曲线终点样式

@property(nonatomic) CGLineCap lineCapStyle; // CGPath.h typedef CF_ENUM(int32_t, CGLineCap) { kCGLineCapButt, kCGLineCapRound, kCGLineCapSquare };




  • 曲线连接点样式

@property(nonatomic) CGLineJoin lineJoinStyle; // CGPath.h typedef CF_ENUM(int32_t, CGLineJoin) { kCGLineJoinMiter, kCGLineJoinRound, kCGLineJoinBevel };




  • 内角和外角距离

@property(nonatomic) CGFloat miterLimit;


2452150-d6647c67c61e87c6.png






  • 渲染精度

@property(nonatomic) CGFloat flatness;

  • 是否使用基偶填充规则 -- 详细介绍

@property(nonatomic) BOOL usesEvenOddFillRule;

  • 虚线

- (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;

  • 重新获取虚线的模式

- (void)getLineDash:(CGFloat *)pattern count:(NSInteger *)count phase:(CGFloat *)phase;

绘制路径

  • 填充路径

- (void)fill;

  • 使用混合模式进行填充

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

  • 绘制路径

- (void)stroke;

  • 使用混合模式进行填充

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

剪切路径

  • 剪切路径

- (void)addClip;

Hit Detection

  • 是否包含某个点

- (BOOL) containsPoint:(CGPoint)point;

  • 路径是否为空

@property (readonly, getter=isEmpty) BOOL empty;

  • 路径覆盖的矩形区域

@property (nonatomic, readonly) CGRect bounds;

Apply Transform

- (void)applyTransform:(CGAffineTransform)transform;

实例






Demo: https://github.com/iOSlixiang/Animations.git

发布人:62b9****    IP:117.173.23.***     举报/删稿
展会推荐
让朕来说2句
评论
收藏
点赞
转发