iOS开发的2D绘制--CoreGraphics的简单使用四(UIBezierPath)
来源:互联网 发布:java 反射 用途 编辑:程序博客网 时间:2024/05/16 14:36
前面几篇文章简单的介绍了使用2D绘制一些简单的使用,这篇文章介绍一个CoreGraphics中比较有意思的类UIBezierPath,为什么说它有意思呢,因为理论上用这个类可以完成任何的2D绘制。它使CoreGraphics框架中,针对绘图路径的一个封装类。
而且有一个mac下的比较著名的软件PaintCode,用来绘图后生成相应Objective-c代码的工具,其生成的代码也是基于UIBezierPath的。
大家可以去查看它的API了解它具体含有的方法,这里给出一个例子。
使用UIBezierPath绘制类似iOS扁平化之前的UINavigationBar返回按钮的效果。
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGFloat topEdgeInset = 10;//图形距顶部的距离
CGFloat leftEdgeInset = 0;//图形距离左侧的距离
CGFloat rightEdgeInset = 0;//图形距离右侧的距离
CGFloat arrowCuspX = leftEdgeInset;//箭头尖的位置的起点
CGFloat leftCornerX = arrowCuspX+10;//箭头尖左侧突出20
CGFloat rightCornerX = self.frame.size.width-rightEdgeInset;
CGFloat topY = topEdgeInset;
CGFloat midY = self.frame.size.height/2;
CGFloat bottomY = self.frame.size.height-topEdgeInset;
CGFloat cornerRadius=6;//右侧的圆角弧度
CGContextRef context = UIGraphicsGetCurrentContext();
//定义填充的颜色,这里我用红色
UIColor* fillColor = [UIColor redColor];
//定义一个渐变颜色,实现上面那个填充色的渐变
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray* innerColorColors = [NSArray arrayWithObjects:
(id)[UIColorwhiteColor].CGColor,
(id)fillColor.CGColor,
(id)[UIColorblackColor].CGColor,nil];
CGFloat innerColorLocations[] = {0,0.3,1};
CGGradientRef innerColor = CGGradientCreateWithColors(colorSpace, (CFArrayRef)innerColorColors, innerColorLocations);
//线条宽度设置为0
CGFloat bezierStrokeWidth = 0;
//UIBezierPath
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(arrowCuspX, midY)];
[bezierPath addLineToPoint:CGPointMake(leftCornerX, bottomY)];
[bezierPath addLineToPoint:CGPointMake(rightCornerX-cornerRadius, bottomY)];
[bezierPath addQuadCurveToPoint:CGPointMake(rightCornerX, bottomY-cornerRadius) controlPoint:CGPointMake(rightCornerX, bottomY)];
[bezierPath addLineToPoint:CGPointMake(rightCornerX, topY+cornerRadius)];
[bezierPath addQuadCurveToPoint:CGPointMake(rightCornerX-cornerRadius, topY) controlPoint:CGPointMake(rightCornerX, topY)];
[bezierPath addLineToPoint:CGPointMake(leftCornerX, topY)];
[bezierPath addLineToPoint:CGPointMake(arrowCuspX, midY)];
[bezierPath closePath];//关闭路径,如果当前没有完成封闭,则在起点和终点补上一条线
CGContextSaveGState(context);//先保存当前绘制
[bezierPath addClip];//把此路径裁剪出来,否则渐变的绘制区域是整个当前的图形上下文
CGContextDrawLinearGradient(context, innerColor,CGPointMake(leftCornerX, topY),CGPointMake(leftCornerX, bottomY), 0);//绘制渐变
CGContextRestoreGState(context);//恢复绘制的内容到之前的的屏幕上
bezierPath.lineWidth = bezierStrokeWidth;
[bezierPathstroke];//路径绘制
//清理释放内存
CGGradientRelease(innerColor);
CGColorSpaceRelease(colorSpace);
}
看一下绘制出来的效果:
希望这个例子能对大家有所帮助,共勉。
- iOS开发的2D绘制--CoreGraphics的简单使用四(UIBezierPath)
- iOS开发的2D绘制--CoreGraphics的简单使用一(画线条)
- iOS开发的2D绘制--CoreGraphics的简单使用二(画图形)
- iOS开发的2D绘制--CoreGraphics的简单使用三(画文字和图片)
- iOS 简单的使用UIBezierPath绘制
- iOS 简单的使用UIBezierPath绘制
- 使用 UIBezierPath 进行简单的图形绘制
- iOS 简单的贝塞尔(UIBezierPath)曲线使用
- iOS 雷达图的绘制 贝塞尔曲线的使用UIBezierPath
- IOS开发:CoreGraphics简单绘图
- 使用CoreGraphics绘制一个简单的折线图,可自定义坐标。
- iOS 使用UIBezierPath 绘制图形
- UIBezierPath + CAShapeLayer的简单使用
- iOS画图-UIBezierPath和CAShapeLayer的简单使用
- 【iOS学习】基于CoreGraphics的3D渲染方案
- iOS 绘制简单图形---UIBezierPath篇
- iOS开发UIBezierPath的详细介绍
- iOS 开发 Quartz 2D+ UIBezierPath绘图大全详解
- Openocd 安装并在Eclipse环境下通过J-Link调试
- 关于64位WIN7下,PL2303导致蓝屏的问题
- 光流法检测——特征点
- Android ADB学习--相关记录
- 本地存储
- iOS开发的2D绘制--CoreGraphics的简单使用四(UIBezierPath)
- PHP获取当前页面完整的URL
- 学习方法
- 电脑突然重启解决方案
- SVN使用中的经验浅谈
- poj 1000 A+B Problem
- 范围潜变和渐进明晰
- hdu - 4619 - Warm up 2
- 对英文单词的词性标注