MKMapView实时绘画渐变线条运动轨迹
2016-01-21 17:58
225 查看
首先你要看官方项目Breadcrumb,Breadcrumb项目中就是实时画运动轨迹的(项目中得到下一个点就移除覆盖物重新绘画所有的点连线);
下面讲下Core Graphics的基本使用
使用Core Graphics来绘图,最简单的就是自定义一个继承UIView类,重写
这个方法就可以了,然后在这个方法了写绘画代码就可以了;
//这里看下官方项目Quartz2D for iOS
最简单的画一条直线
开始实现思路:ab两点线条,画一个ABCD连线矩形
最后如何在地图上画渐变的线条(思路来源)
渐变线条如下:
到这里渐变线条就会画了,地图上绘画就OK!
下面讲下Core Graphics的基本使用
使用Core Graphics来绘图,最简单的就是自定义一个继承UIView类,重写
/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */
这个方法就可以了,然后在这个方法了写绘画代码就可以了;
//这里看下官方项目Quartz2D for iOS
最简单的画一条直线
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文 CGContextMoveToPoint(ctx, 20, 20); CGContextAddLineToPoint(ctx, 100, 200); CGContextSetLineWidth(ctx, 50); //注意,线条只能画成是空心的 CGContextStrokePath(ctx); }画一个渐变图形Quartz2D for iOS部分代码
-(void)drawInContext:(CGContextRef)context { // Draw the colored pattern. Since we have a CGColorRef for this pattern, we just set // that color current and draw. CGContextSetFillColorWithColor(context, self.coloredPatternColor); CGContextFillRect(context, CGRectMake(10.0, 10.0, 90.0, 90.0)); // You can also stroke with a pattern. CGContextSetStrokeColorWithColor(context, self.coloredPatternColor); CGContextStrokeRectWithWidth(context, CGRectMake(120.0, 10.0, 90.0, 90.0), 8.0); // Since we aren't encapsulating our pattern in a CGColorRef for the uncolored pattern case, setup requires two steps. // First you have to set the context's current colorspace (fill or stroke) to a pattern colorspace, // indicating to Quartz that you want to draw a pattern. CGContextSetFillColorSpace(context, self.uncoloredPatternColorSpace); // Next you set the pattern and the color that you want the pattern to draw with. CGFloat color1[] = {1.0, 0.0, 0.0, 1.0}; CGContextSetFillPattern(context, self.uncoloredPattern, color1); // And finally you draw! CGContextFillRect(context, CGRectMake(10.0, 120.0, 90.0, 90.0)); // As long as the current colorspace is a pattern colorspace, you are free to change the pattern or pattern color CGFloat color2[] = {0.0, 1.0, 0.0, 1.0}; CGContextSetFillPattern(context, self.uncoloredPattern, color2); CGContextFillRect(context, CGRectMake(10.0, 230.0, 90.0, 90.0)); // And of course, just like the colored case, you can stroke with a pattern as well. CGContextSetStrokeColorSpace(context, self.uncoloredPatternColorSpace); CGContextSetStrokePattern(context, self.uncoloredPattern, color1); CGContextStrokeRectWithWidth(context, CGRectMake(120.0, 120.0, 90.0, 90.0), 8.0); // As long as the current colorspace is a pattern colorspace, you are free to change the pattern or pattern color CGContextSetStrokePattern(context, self.uncoloredPattern, color2); CGContextStrokeRectWithWidth(context, CGRectMake(120.0, 230.0, 90.0, 90.0), 8.0); } -(void)dealloc { CGColorRelease(_coloredPatternColor); CGPatternRelease(_uncoloredPattern); CGColorSpaceRelease(_uncoloredPatternColorSpace); }来到这里出现一个不解的地方,线条那怎么画呢?
开始实现思路:ab两点线条,画一个ABCD连线矩形
实现代码: CGContextSaveGState(context); //水平长度 int i=0; float level=MAX([self pointForMapPoint:points[i]].x, [self pointForMapPoint:points[i+1]].x)-MIN([self pointForMapPoint:points[i]].x,[self pointForMapPoint:points[i+1]].x); //垂直长度 float vertical=MAX([self pointForMapPoint:points[i]].y, [self pointForMapPoint:points[i+1]].y)-MIN([self pointForMapPoint:points[i]].y,[self pointForMapPoint:points[i+1]].y); //两点直线长度 float distance=sqrt(pow(level, 2)+pow(vertical, 2)); float minlevel=vertical/distance*10; float minvertical=level/distance*10; CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, [self pointForMapPoint:points[i]].x+minlevel, [self pointForMapPoint:points[i]].y-minvertical); CGPathAddLineToPoint(path, NULL, [self pointForMapPoint:points[i+1]].x+minlevel, [self pointForMapPoint:points[i+1]].y-minvertical); CGPathAddLineToPoint(path, NULL, [self pointForMapPoint:points[i+1]].x-minlevel, [self pointForMapPoint:points[i+1]].y+minvertical); CGPathAddLineToPoint(path, NULL, [self pointForMapPoint:points[i]].x-minlevel, [self pointForMapPoint:points[i]].y+minvertical); CGPathCloseSubpath(path); CGContextAddPath(context, path); CGFloat locations[2] = {0.0,1.0}; CGColorRef startColor; CGColorRef endColor; startColor=[UIColor greenColor].CGColor; endColor=[UIColor redColor].CGColor; NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; colors=@[(__bridge id) startColor, (__bridge id) endColor]; CGGradientRef gradient=CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); //渐变区域裁剪 CGContextClip(context); //绘制渐变 CGContextDrawLinearGradient(context, gradient, sPoint, ePoint, kCGGradientDrawsAfterEndLocation); CGContextRestoreGState(context);// 恢复到之前的context //释放对象 CGColorSpaceRelease(colorSpace); CGGradientRelease(gradient); CGPathRelease(path);
最后如何在地图上画渐变的线条(思路来源)
渐变线条如下:
CGContextRef ctx=UIGraphicsGetCurrentContext(); CGColorRef startColor; CGColorRef endColor; startColor=[UIColor greenColor].CGColor; endColor=[UIColor redColor].CGColor; NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient = CGGradientCreateWithColors(baseSpace, (__bridge CFArrayRef)colors, NULL); CGColorSpaceRelease(baseSpace), baseSpace = NULL; CGContextMoveToPoint(ctx, 30, 30); CGContextAddLineToPoint(ctx, 300, 300); //注意,线条只能画成是空心的 CGContextSetLineWidth(ctx, 50); CGContextSetLineJoin(ctx, kCGLineJoinRound); CGContextSetLineCap(ctx, kCGLineCapRound); CGContextReplacePathWithStrokedPath(ctx); CGContextClip(ctx); CGContextDrawLinearGradient(ctx, gradient, CGPointMake(30, 30), CGPointMake(300, 300), 0); CGGradientRelease(gradient), gradient = NULL;
到这里渐变线条就会画了,地图上绘画就OK!
相关文章推荐
- Unity3D 制作场景地图 插件
- CSS @font-face(CSS 自定义字体)
- Poj 1472 Instant Complexity
- CentOS6.5下keepalived+nginx实现双机热备
- 关于数据降维的一些经典算法
- Android Studio上修改SVN地址
- AndroidStudio插件GsonFormat快速实现JavaBean
- rsyslog日志管理(mariadb+loganalyzer)
- 了解lua setmetatable和__index
- 图像旋转原理及实现
- IDA6.6远程调试Android应用程序的一点总结
- Android学习历程8-Fragment详解
- 【转】分区表索引实践案例
- 小技巧:域账号变更信息筛选\用户数据筛选
- 小技巧:域账号变更信息筛选\用户数据筛选
- Git 常用命令
- 关于Bitmap对象,尺寸压缩,质量压缩
- leetcode21. Merge Two Sorted Lists
- weinre使用
- python学习------------异常以及反射