iOS开发 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;
以上两个方法都可以画矩形
1.这个工厂方法画矩形 但是这个矩形是可以画圆角的 第一个参数是矩形 第二个参数是圆角大小。
2.这个工厂方法画矩形 可以指定某一个角画成圆角 可以用来给UIView扩展添加圆角方法。
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;
画圆弧 参数如下
center:弧线所在圆的圆心
radius:弧线所在圆的半径
startAngle:开始角度值
endAngle:弧线结束角度值
clockwise:是否顺时针画弧线
- (UIBezierPath *)bezierPathByReversingPath
创建 并返回一个与当前路径相反的新的贝塞尔路径对象
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
通过一个CGPath来创建一个贝塞尔路径对象
创建路径
- (void)moveToPoint:(CGPoint)point
将接收器的当前点移动到指定位置。
- (void)addLineToPoint:(CGPoint)
point
附加一条直线到接收器的路径。
radius:(CGFloat)- (void)addArcWithCenter:(CGPoint)
centerradius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise
追加一个弧到接收器的路径。
controlPoint1:(CGPoint)- (void)addCurveToPoint:(CGPoint)
endPointcontrolPoint1
controlPoint2:(CGPoint)controlPoint2
追加一个一次Bezier曲线到接收器的路径。
controlPoint:(CGPoint)- (void)addQuadCurveToPoint:(CGPoint)
endPointcontrolPoint
追加一个二次Bezier曲线到接收器的路径。
- (void)closePath
关闭最近添加的子路径。
- (void)removeAllPoints
删除从接收点开始的所有的点,有效地删除所有子路径。
- (void)appendPath:(UIBezierPath *)
bezierPath
追加指定的路径对象的内容到接收器的路径。
@property(nonatomic) CGPathRef CGPath
路径的核心绘图的表示。
@property(nonatomic, readonly) CGPoint currentPoint
图形路径中的当前点。(只读)
访问绘图属性
@property(nonatomic) CGFloat lineWidth
path的行宽
@property(nonatomic) CGLineCap lineCapStyle线条拐角
当填充时 路径结束点的路径形状 举例子说 圆形的顶部等。
@property(nonatomic) CGLineJoin lineJoinStyle
终点处理
@property(nonatomic) CGFloat miterLimit
设置线段链接处的样式
@property(nonatomic) CGFloat flatness
确定弯曲路径段的绘制精度的因素。
@property(nonatomic) BOOL usesEvenOddFillRule
一个bool值 指定even-odd规则是否在path可用
- (void)setLineDash:(const CGFloat *)pattern
count:(NSInteger)count
phase:(CGFloat)phase
设置path的接触样式。
- (void)getLineDash:(CGFloat *)pattern
count:(NSInteger*)count
phase:(CGFloat *)phase
获取path的线接触样式
画路径
- (void)fill
用当前绘图属性绘制当前被路径包围的区域
- (void)fillWithBlendMode:(CGBlendMode)blendMode
alpha:(CGFloat)alpha
用指定的混合模式和透明度值来描绘受接收路径所包围的区域。
- (void)stroke
利用当前绘图属性沿着接收器的路径绘制一行
- (void)strokeWithBlendMode:(CGBlendMode)blendMode
alpha:(CGFloat)alpha
使用指定的混合模式和透明度值
沿着接收器路径。绘制一行
裁剪路径
- (void)addClip
剪切被接收者路径包围的区域 该路径是带有剪切路径的当前绘图上下文。使得其成为我们当前的剪切路径、
检测
- (BOOL)containsPoint:(CGPoint)point
返回一个布尔值,该值指示接收器所包含的区域是否包含指定的点。
@property(readonly, getter=isEmpty) BOOL empty
一个布尔值,该值指示路径是否有任何有效的元素。(只读)
@property(nonatomic, readonly) CGRect bounds
路径的包围矩形。(只读)
提交变换
- (void)applyTransform:(CGAffineTransform)transform
用指定的仿射变换矩阵变换路径中的所有点。
常数
enum{
UIRectCornerTopLeft= 1 << 0,
UIRectCornerTopRight= 1 << 1,
UIRectCornerBottomLeft= 1 << 2,
UIRectCornerBottomRight= 1 << 3,
UIRectCornerAllCorners= ~0
};
typedefNSUInteger UIRectCorner;
矩形的角。
UIRectCornerTopLeft 左上角
UIRectCornerTopRight 右上角
UIRectCornerBottomLeft 左下角
UIRectCornerBottomRight 右下角
UIRectCornerAllCorners 所有的角。
小例子
以下方法均在drawRect方法中调用
画三角形
- -(void)drawTriangle
- {
- //创建路径对象
- UIBezierPath * path = [UIBezierPath bezierPath];
- //path的设置
- [path moveToPoint:CGPointMake(20, 20)];
- [path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)];
- [path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)];
- //闭合曲线 调用closePath自动生成 也可以调用-addLineToPoint:生成。
- [path closePath];
- //设置填充颜色
- UIColor * color = [UIColor redColor];
- [color set];
- [path fill];
- //设置画笔颜色
- UIColor * strokeColor = [UIColor blueColor];
- [strokeColor set];
- //根据设置的各个点连线
- [path stroke];
- path.lineWidth = 1.2;
- }
其中设置画笔颜色通过set方法。
如果我们需要设置填充颜色,那么需要在设置画笔颜色之前先设置填充颜色,否则画笔颜色就被填充颜色取代了。也就是说 如果要让填充颜色和画笔颜色不一样 那么必须先设置填充颜色再设置画笔颜色。
画矩形
- -(void)drawRectangle
- {
- CGRect rect = CGRectMake(30, 30, 200, 200);
- UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];
- path.lineWidth = 1.2;
- path.lineCapStyle = kCGLineCapRound;
- path.lineJoinStyle = kCGLineJoinBevel;
- //设置填充颜色
- UIColor *fillColor = [UIColor greenColor];
- [fillColor set];
- [path fill];
- UIColor *strokeColor = [UIColor blueColor];
- [strokeColor set];
- [path stroke];
- }
lineCapStyle属性是用来设置线条拐角冒的样式的
/* Line cap styles. */
typedef CF_ENUM(int32_t, CGLineCap) {kCGLineCapButt,kCGLineCapRound,kCGLineCapSquare
};
第一个是默认的,第二个是轻微圆角,第三个是正方形
lineJoinStyle
属性是用来设置两条线连结点的样式,其中也有三个选择:
/* Line join styles. */typedef CF_ENUM(int32_t, CGLineJoin) {kCGLineJoinMiter,kCGLineJoinRound,kCGLineJoinBevel
};
第一个默认表示斜接,第二个圆滑衔接,第三个斜角衔接。 画圆
- -(void)drawCircle
- {
- CGRect rect = CGRectMake(20, 20, 200, 200);
- UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:rect];
- //设置填充颜色
- UIColor * fillColor = [UIColor greenColor];
- [fillColor set];
- [path fill];
- //设置画笔颜色
- UIColor * strokeColor = [UIColor blueColor];
- [strokeColor set];
- [path stroke];
- }
画椭圆
- -(void)drawEllipse
- {
- CGRect rect = CGRectMake( 20, 20, 200, 100);
- UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:rect];
- UIColor * fillColor = [UIColor redColor];
- [fillColor set];
- [path fill];
- UIColor * strokeColor = [UIColor greenColor];
- [strokeColor set];
- [path stroke];
- }
画带圆角的矩形
- -(void)drawRoundRect
- {
- CGRect rect = CGRectMake(20, 20, 200, 200);
- UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:15];
- UIColor * fillColor = [UIColor redColor];
- [fillColor set];
- [path fill];
- UIColor * strokeColor = [UIColor greenColor];
- [strokeColor set];
- [path stroke];
- }
画指定几个圆角的矩形
- -(void)drawSomeRoundRect
- {
- CGRect rect = CGRectMake(20, 20, 200, 200);
- UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(30, 10)];
- UIColor * fillColor = [UIColor redColor];
- [fillColor set];
- [path fill];
- UIColor * strokeColor = [UIColor grayColor];
- [strokeColor set];
- [path stroke];
- }
画圆弧 画弧前,我们需要了解其参考系,如下图(图片来自网络):
- -(void)drawARCPath
- {
- const CGFloat pi = M_PI;
- NSLog(@"%f",pi);
- CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
- CGFloat end = DefreesToRadius(140) ;
- NSLog(@"%f",end);
- UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:end clockwise:YES];
- UIColor * fillColor = [UIColor redColor];
- [fillColor set];
- [path fill];
- path.lineCapStyle = kCGLineCapRound;
- path.lineJoinStyle = kCGLineJoinRound;
- path.lineWidth = 5.0;
- UIColor * strokeColor = [UIColor blueColor];
- [strokeColor set];
- [path stroke];
- }
画二次贝塞尔曲线 先来学习一下关于控制点,如下图(图片来自网络):
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
参数说明:
endPoint
:终端点<br/>
controlPoint
:控制点,对于二次贝塞尔曲线,只有一个控制点
- -(void)drawBezierPath
- {
- UIBezierPath * path = [UIBezierPath bezierPath];
- [path moveToPoint:CGPointMake(30, 200)];
- [path addQuadCurveToPoint:CGPointMake(100, 20) controlPoint:CGPointMake(10, 10)];
- UIColor * strokeColor = [UIColor redColor];
- [strokeColor set];
- [path stroke];
- }
画二次贝塞尔曲线的步骤:
- 先设置一个起始点,也就是通过
-moveToPoint:
设置 - 调用
-addQuadCurveToPoint:controlPoint:
方法设置终端点和控制点,以画二次曲线
sin
或者cos
函数吧?这两个只是特例而已,其实可以画任意图形,只是想不到,没有做不到的。
画三次贝塞尔曲线
贝塞尔曲线必定通过首尾两个点,称为端点;中间两个点虽然未必要通过,但却起到牵制曲线形状路径的作用,称作控制点。关于三次贝塞尔曲线的控制器,看下图:
提示:其组成是起始端点+控制点1+控制点2+终止端点
如下方法就是画三次贝塞尔曲线的关键方法,以三个点画一段曲线,一般和-moveToPoint:
配合使用。
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
- - (void)drawThirdBezierPath {
- UIBezierPath *path = [UIBezierPath bezierPath];
- // 设置起始端点
- [path moveToPoint:CGPointMake(20, 150)];
- [path addCurveToPoint:CGPointMake(300, 150)
- controlPoint1:CGPointMake(160, 0)
- controlPoint2:CGPointMake(160, 250)];
- path.lineCapStyle = kCGLineCapRound;
- path.lineJoinStyle = kCGLineJoinRound;
- path.lineWidth = 5.0;
- UIColor *strokeColor = [UIColor redColor];
- [strokeColor set];
- [path stroke];
- }
我们需要注意,这里确定的起始端点为(20,150),终止端点为(300, 150),基水平方向是一致的。控制点1的坐标是(160,0),水平方向相当于在中间附近,这个参数可以调整。控制点2的坐标是(160,250),如果以两个端点的连线为水平线,那么就是250-150=100,也就是在水平线下100。这样看起来就像一个sin
函数了。
iOS开发 UIBezierPath曲线动画相关推荐
- iOS开发 QQ粘性动画效果
QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00 博客园精华区 原文 http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...
- iOS开发之各种动画各种页面切面效果
转发:http://www.cocoachina.com/ios/20141226/10775.html 今天所介绍的主题是关于动画的,在之前的博客中也有用到动画的地方,今天就好好的总结一下iOS开发 ...
- iOS开发 贝塞尔曲线UIBezierPath
2019独角兽企业重金招聘Python工程师标准>>> UIBezierPath基础 UIBezierPath对象是CGPathRef数据类型的封装.每一个直线段或者曲线段的结束的地 ...
- iOS开发 贝塞尔曲线UIBezierPath(后记)
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- IOS开发-画曲线画弧线画圆
(1)导入QuartzCore框架 (2)自定义一个view,然后输入下面的代码: - (void)drawRect:(CGRect)rect {[self drawArc1]; }/*** 曲线*/ ...
- iOS 开发——登录页面动画、转场动画
DEMO下载地址:https://github.com/YYProgrammer/YYLoginTranslationDemo 技术点分析 如何生成一个动画让控件执行? 现流行的方式主要有三种: 1. ...
- (0073)iOS开发之核心动画高级技巧
核心动画高级技巧丛书 https://zsisme.gitbooks.io/ios-/content/chapter6/careplicatorLayer.html
- iOS开发系列--让你的应用“动”起来--超详细的ios核心动画介绍
概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
[在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要"参与到"动画中 [UIView beginAnimations: ...
最新文章
- Android 5.0新特性ToolBar
- git的基本使用命令操作
- Asp.Net Core 视图整理(一)
- 机器学习系列之神经网络入门基础知识
- 深入理解Oracle表(5):三大表连接方式详解之Hash Join的定义,原理,算法,成本,模式和位图
- 给zuul网关添加过滤器
- 微服务和分布式学习专栏
- php中使用httpclient
- 二级vb笔试题库__全国计算机等级考试,2012年9月全国计算机二级VB笔试标准预测试卷试题六...
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 4)
- html文本框最小数字,HTML5中的数字类型的输入框:数字选择器
- python 修改列名_请教一个 Python 技巧(批量对 pandas.DataFrame()统一修改列名)
- 艾伟:为WPF项目创建单元测试
- Oracle_用户管理
- 数据结构笔记(参考王道考研系列)
- 计算机培训教学计划,计算机培训教学计划.doc
- win7怎么把计算机放到桌面6,手机投屏到电脑win7最简单具体操作步骤
- poi excel 隐藏行
- html给标签添加背景,HTML – 如何在div上添加背景(灯箱效果)?
- ctf题库--天下武功唯快不破
热门文章
- 就业协议服务器是什么意思,什么是就业协议
- android 摄像头 ip,摄像头已连接,但没有发生任何事情,openCV-IP摄像头Android
- bzoj3047: Freda的传呼机bzoj2125: 最短路
- 怎样防止恶意刷短信验证?
- IDEA 关于两个分支代码合并的操作
- 服务器如何从安全模式增加用户名,win10安全模式里怎么添加账户_win10 安全模式如何添加用户-win7之家...
- 人工智能:智慧环保(上海AI智慧环保,绿色新时代)
- VS code实现前后端交互及大屏可视化(Flask+Mysql+Echarts+HTML+JS+CSS)
- Linux系统裁剪summary
- Google云计算之Dremel