照例,我们看下本文的学习大纲

在该篇文章中可以了解到知识点有以下
- UIBezierPath的概念
- UIBezierPath的基本使用方法
- UIBezierPath的绘制案例
UIBezierPath的概念
UIBezierPath这个类在UIKit中, 是Core Graphics框架关于path的一个封装,使用此类可以定义简单的形状,比如我们常用到,矩形,圆形,椭圆,弧,或者不规则的多边形。
UIBezierPath的基本使用方法
-
UIBezierPath对象是CGPathRef数据类型的封装。
关于CGPathRef不了解,可以阅读上篇文章。
Quartz2D的详解和使用 -
path如果是基于矢量形状的,都用直线或曲线去创建。
-
一般使用UIBezierPath都是在重写view的drawRect方法这种情形。
UIBezierPath的绘制案例
1.绘制直线

-(void)drawTwoLine
{ // 创建路径
UIBezierPath * path = [UIBezierPath bezierPath];
// 创建起点
[path moveToPoint:CGPointMake(10, 10)];
// 添加线段到终点
[path addLineToPoint:CGPointMake(90, 90)];
[path moveToPoint:CGPointMake(90, 10)];
[path addLineToPoint:CGPointMake(10, 90)];
path.lineWidth = 10.f;// 设置线宽
path.lineCapStyle = kCGLineCapSquare;// 设置线头样式
path.lineJoinStyle = kCGLineJoinBevel;// 设置交叉样式
[path stroke];// 渲染
}
2.绘制扇形

-(void)drawFan
{
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(40, 40) radius:30 startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
// 绘制扇形时需要连接圆心
[path addLineToPoint:CGPointMake(40, 40)];
[[UIColor cyanColor] set];
[path fill]; // 填充
// [path addClip];
}[图片上传中...(圆角矩形.png-d73dcf-1516616860954-0)]
3.绘制圆角矩形

-(void)drawCornerRect
{
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 150, 100) cornerRadius:20];
[path stroke];
}
4.绘制椭圆

-(void)drawOvalUI
{
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50,50, 50, 50)];
// [path stroke];
[[UIColor cyanColor] set];
[path fill];
}
5.绘制圆弧或圆形
-(void)drawArcUI
{
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(40, 40) radius:30 startAngle:M_PI_2 endAngle:0 clockwise:YES];
// [path addLineToPoint:CGPointMake(40, 40)];
[[UIColor cyanColor] set];
[path stroke];// 描边
// [path fill]; // 填充
}
6.绘制图片
方式1

UIImage * image = [UIImage imageNamed:@"1"];
[image drawInRect:CGRectMake(20, 20, 100, 100)];// 按照frame显示大小
方式二

UIImage * image = [UIImage imageNamed:@"1"];
[image drawAtPoint:CGPointMake(20, 20)]; // 以这个坐标起点,到view的边缘
方式三
[图片上传中...(图片3.png-dc1f9e-1516617025868-0)]
UIImage * image = [UIImage imageNamed:@"1"];
[image drawAsPatternInRect:CGRectMake(0, 0, 100, 100)];// 如果图片比区域小,会进行平铺;如果图片比区域大,有多少绘制多少
8.绘制文字
绘制文字1

NSString * str = @"韩寒,这个世界的冒犯者,还是和世界和解了。";
NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName:[UIColor grayColor],NSStrokeWidthAttributeName:@10};
// 绘制方式1
[str drawInRect:CGRectMake(0, 0, 120, 100) withAttributes:dic];
绘制文字2

NSString * str = @"韩寒,这个世界的冒犯者,还是和世界和解了。";
NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName:[UIColor grayColor],NSStrokeWidthAttributeName:@10};
// 绘制方式2
[str drawAtPoint:CGPointMake(0, 70) withAttributes:dic];
9.保存图片
注意事项:ios 8.0之后,添加权限提示。

- (IBAction)saveImage:(id)sender {
// 开启图片context
UIGraphicsBeginImageContextWithOptions(self.myImage.bounds.size, NO, 0);
// 获取图片的范围
[self.myImage drawViewHierarchyInRect:self.myImage.bounds afterScreenUpdates:YES];
// 从上下文context中获取图片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//结束context,记得关闭
UIGraphicsEndImageContext();
// 写入相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
/*
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.
ios 8.0之后,添加权限提示
*/
}
//系统指定的保存后结束要执行的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
贝塞尔曲线简单应用git地址
[end]
关于UIBezierPath的简单使用到这里介绍结束。
网友评论