iOS UIBezierPath简单实用
1.介绍
UIBezierPath这个类在UIKit中, 是Core Graphics框架关于path的一个封装,使用此类可以定义简单的形状,比如我们常用到,矩形,圆形,椭圆,弧,或者不规则的多边形
每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
我们一般使用UIBezierPath都是在重写view的drawRect方法这种情形。我们用直线去创建矩形或多边形,使用曲线创建弧或者圆。
使用步骤:
1、 重写View的drawRect方法
2、 创建UIBezierPath的对象
3、 使用方法moveToPoint: 设置初始点
4、 根据具体要求使用UIBezierPath类方法绘图(比如要画线、矩形、圆、弧?等)
5、 设置UIBezierPath对象相关属性 (比如lineWidth、lineJoinStyle、aPath.lineCapStyle、color)
6、 使用stroke 或者 fill方法结束绘图
在介绍其他使用方法之前,我们先来看一下 path 的几个属性,以便下面我进行设置。
1、 [color set]; 设置线条颜色,也就是相当于画笔颜色
2、 path.lineWidth = 5.0; 这个很好理解了,就是划线的宽度
3、 path.lineCapStyle 这个线段起点是终点的样式,这个样式有三种:
(
1、 kCGLineCapButt 该属性值指定不绘制端点, 线条结尾处直接结束。这是默认值。
2、 kCGLineCapRound 该属性值指定绘制圆形端点, 线条结尾处绘制一个直径为线条宽度的半圆。
3、 kCGLineCapSquare 该属性值指定绘制方形端点。 线条结尾处绘制半个边长为线条宽度的正方形。需要说明的是,这种形状的端点与“butt”形状的端点十分相似,只是采用这种形式的端点的线条略长一点而已
)
4、 path.lineJoinStyle 这个属性是用来设置两条线连结点的样式,同样它也有三种样式供我们选择
(
1、 kCGLineJoinMiter 斜接
2、 kCGLineJoinRound 圆滑衔接
3、 kCGLineJoinBevel 斜角连接
)
5、 [path stroke]; 用 stroke 得到的是不被填充的 view , [path fill]; 用 fill 得到的内部被填充的 view,这点在下面的代码还有绘制得到的图片中有,可以体会一下这两者的不同。
2.一些简单使用代码
(1) 直线
- (void)drawView1 {
UIColor *color = [UIColor redColor];
[color set]; //设置线条颜色
UIBezierPath *myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:CGPointMake(200, 50)];
[myPath addLineToPoint:CGPointMake(300, 100)];
myPath.lineWidth = 5.0;
myPath.lineCapStyle = kCGLineCapRound; //线条拐角
myPath.lineJoinStyle = kCGLineJoinRound; //终点处理
[myPath stroke];
}
(2) 多边形
- (void)drawView2 {
UIColor *color = [UIColor redColor];
[color set]; //设置线条颜色
UIBezierPath *myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:CGPointMake(200, 50)];
[myPath addLineToPoint:CGPointMake(300, 100)];
[myPath addLineToPoint:CGPointMake(260, 200)];
[myPath addLineToPoint:CGPointMake(100, 200)];
[myPath addLineToPoint:CGPointMake(100, 70)];
[myPath closePath];
myPath.lineWidth = 5.0;
myPath.lineCapStyle = kCGLineCapRound; //线条拐角
myPath.lineJoinStyle = kCGLineJoinRound; //终点处理
// [myPath fill];//颜色填充
[myPath stroke]; //内部无色
//closePath方法不仅结束一个shape的subpath表述,它也在最后一个点和第一个点之间画一条线段,这个一个便利的方法我们不需要去画最后一条线了
}
(3) 矩形正方形 和一些特殊
- (void)drawView3 {
UIColor *color = [UIColor redColor];
[color set];
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)];
// 带弧度的矩形
// UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 15, 100, 80) cornerRadius:10];
// 部分圆角
UIBezierPath *path =[UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 45, 90, 90)
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft
cornerRadii:CGSizeMake(10, 10)];
path.lineWidth = 5.0;
path.lineCapStyle = kCGLineCapRound; //线条拐角
path.lineJoinStyle = kCGLineJoinRound; //终点处理
[path stroke];
}
(4) 椭圆 圆 + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
- (void)drawView4 {
UIColor *color = [UIColor redColor];
[color set];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 100, 80)];
path.lineWidth = 5.0;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
[path stroke];
}
(5) 弧线 + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwis
/*
ArcCenter: 原点
radius: 半径
startAngle: 开始角度
endAngle: 结束角度 弧度制 X轴正方形为0 圆2*3.14
clockwise: 是否顺时针方向 /
*/
- (void)drawView5 {
UIColor *color = [UIColor redColor];
[color set];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:90 startAngle:0 endAngle:DEGREES_TO_RADIANS(90) clockwise:YES];
path.lineWidth = 5.0;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
[path stroke];
}
(6) 二级贝塞尔曲线 - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
// 这个方法绘制二次贝塞尔曲线。曲线段在当前点开始,在指定的点结束,
/*
endpoint 终点 controlPoint 切点 两条曲线的切线的交点
*/ - (void)drawView6 {
UIColor *color = [UIColor redColor];
[color set]; UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = 5.0;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:CGPointMake(40, 150)];
[path addQuadCurveToPoint:CGPointMake(30, 400) controlPoint:CGPointMake(120, 40)];
[path stroke]; }
(7)绘制三次贝塞尔曲线 - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
// 这个方法绘制三次贝塞尔曲线。曲线段在当前点开始,在指定的点结束,两个控制点的切线定义 /*
endPoint 终点 controlPoint1 与起点连线的线与弧线相切 controlPoint2 与终点连线的线与弧线相切
*/ - (void)drawView7 {
UIColor *color = [UIColor redColor];
[color set]; UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5.0;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:CGPointMake(40, 150)];
[path addCurveToPoint:CGPointMake(260, 200)
controlPoint1:CGPointMake(140, 0)
controlPoint2:CGPointMake(140, 400)];;
[path stroke];
}
iOS UIBezierPath简单实用的更多相关文章
- ios runtime简单实用(添加动态属性)
#import "Person.h" @interface Person (PersonCategory) // 添加Person中没有的name属性 @property (n ...
- iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信
一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...
- iOS CAReplicatorLayer 简单动画
代码地址如下:http://www.demodashi.com/demo/11601.html 写在最前面,最近在看学习的时候,偶然间发现一个没有用过的Layer,于是抽空研究了下,本来应该能提前记录 ...
- 最新 AFNetworking 3.0 简单实用封装
AFNetworking 3.0 的到来使我们开发者又方便了许多,话不多说,直接上代码. 1.首先 引入框架AFNetworking框架 GitHub下载地址:https://github.com/A ...
- hook框架frida的安装以及简单实用案例
1.下载地址 https://github.co/frida/frida/releases 2.另外两种安装方法 1.Install from prebuilt binaries This is th ...
- jQuery的几种简单实用效果
许久未分享博客,或许已生疏. 闲来无事, 分享几个jQuery简单实用的效果案例 不喜勿喷... 1.页面常用的返回顶部 <!DOCTYPE html> <html lang=&qu ...
- iOS上简单推送通知(Push Notification)的实现
iOS上简单推送通知(Push Notification)的实现 根据这篇很好的教程(http://www.raywenderlich.com/3443/apple-push-notification ...
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- 简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注 ...
- php简单实用的操作文件工具类(创建、移动、复制、删除)
php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) { // 原目录,复制到的目录 $dir = opend ...
随机推荐
- Aug. 2023 普及组模拟赛 3
题面 T1 最大生成树 Meaning 给定一个完全图,两点之间的边权为这两个点点权之差的绝对值,求这个图的最大生成树. Solution 对于最小生成树,我们可以考虑 Kruskal 算法. Kru ...
- Codeforces Round #620 (Div. 2) ABC 题解
A. Two Rabbits 题意:数轴上有x,y,且x<y.x可以每次+a,y可以每次-b.问能否xy相遇. 思路:只要xy差值是a+b的倍数即可. view code #include< ...
- 借助ETLCloud工具,轻松同步Doris数据至Inceptor数据库
一.背景 在现代企业中,数据是决策和运营的核心.为了更好地利用这些数据,企业通常需要将数据从不同的源系统(如Doris)同步到一个集中的数据仓库(如Inceptor).ETL(Extract, Tra ...
- SciTech-Mathematics-Probability+Statistics-{Problem,Study,Experiment,Conclusion}-Variables: Confounding/Controlled/{Antecedent,Manipulated,Moderating,Intervening,Response}/Extraneous
Problem>Study>Experiment>Conclusion Study: Communication and Networking: Beliefs, Interests ...
- SciTech-EECS-电路设计-PCB设计-原理图设计-Schematic library(原理图元器件库)的设置 及 为新 “Component(元器件)” 新增“Symbol(符号封装)
Altium官方的"原理图设计"文档: How to Create a PCB Schematic | Altium Designer, Zachariah Peterson | ...
- babylon.js 学习笔记(2)
如何在网页中嵌入设计好的模型? 接上回继续,我们设计好精美的模型后,最终总要展示给客户,比如利用playground画了1个方块: const createScene = () => { con ...
- tmux 常用命令
Tmux(Terminal Multiplexer)是一个 终端复用工具,用于在单个终端窗口中创建和管理多个 虚拟终端会话.它主要解决以下问题: Tmux 的核心功能 会话持久化 即使断开SSH连接, ...
- 堆priority_queue
#include<queue> //#include<bits/stdc++.h> using namespace std; priority_queue<int> ...
- LangGraph官方文档笔记——4.提示聊天机器人
目录 前言 官方资料 实践人工协助聊天机器人 创建可以人工协助的聊天机器人 前言 Agent 可能不可靠,并且可能需要人工输入才能成功完成任务.因此,您可能希望在运行之前需要人工批准,以确保一切按预期 ...
- 扣子Coze实战:零基础搭建数据分析智能体,1分钟完成复盘,流量翻10倍
大家好,我是汤师爷,专注AI智能体分享,致力于帮助100W人用智能体创富. 为什么我的视频数据表现总是不尽人意? 明明花了很多时间和精力制作内容,但播放量和互动数据却始终上不去? 每次看数据面板都是一 ...