ios – 绘制MKMapView Overlay,如Google Maps Directions

前端之家收集整理的这篇文章主要介绍了ios – 绘制MKMapView Overlay,如Google Maps Directions前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
iOS 5改变了内置Google Maps App绘制路线的方式:

我现在想在我自己的应用程序中复制路由叠加层的设计,但我目前只能绘制一条纯蓝色的线条.我想添加渐变,边框和辉光的3D效果.关于如何实现这一点的任何想法?

目前我正在使用以下代码

  1. CGContextSetFillColorWithColor(context,fillColor.CGColor);
  2. CGContextSetLineJoin(context,kCGLineJoinRound);
  3. CGContextSetLineCap(context,kCGLineCapRound);
  4. CGContextSetLineWidth(context,lineWidth);
  5. CGContextAddPath(context,path);
  6. CGContextReplacePathWithStrokedPath(context);
  7. CGContextFillPath(context);

导致一个相当丑陋的行:

谢谢!

更新:该解决方案应该适用于iOS 4.0及以上版本.

解决方法

我认为@ChrisMiles是正确的,因为段可能是单独绘制. (我最初以为这可能是使用CGPatternRef可行的,但是您没有访问模式绘图回调中的CTM或路径端点.)

考虑到这一点,这里是一个非常粗糙的,背后的例子,说明你可能如何开始这样的努力(单独填写段落).注意:

>渐变颜色被猜到
>端盖不存在,需要单独实现
>还有一些混叠文物
>没有很大的注意力付出了表现

希望这可以让你开始至少(并通过一些分析几何).

  1. - (CGGradientRef)lineGradient
  2. {
  3. static CGGradientRef gradient = NULL;
  4. if (gradient == NULL) {
  5. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  6. CGColorRef white = [[UIColor colorWithWhite:1.f
  7. alpha:0.7f] CGColor];
  8. CGColorRef blue = [[UIColor colorWithRed:0.1f
  9. green:0.2f
  10. blue:1.f
  11. alpha:0.7f] CGColor];
  12. CGColorRef lightBlue = [[UIColor colorWithRed:0.4f
  13. green:0.6f
  14. blue:1.f
  15. alpha:0.7f] CGColor];
  16. CFMutableArrayRef colors = CFArrayCreateMutable(kcfAllocatorDefault,8,NULL);
  17. CFArrayAppendValue(colors,blue);
  18. CFArrayAppendValue(colors,white);
  19. CFArrayAppendValue(colors,lightBlue);
  20. CFArrayAppendValue(colors,blue);
  21. CGFloat locations[8] = {0.f,0.08f,0.14f,0.21f,0.29f,0.86f,0.93f,1.f};
  22. gradient = CGGradientCreateWithColors(colorSpace,colors,locations);
  23. CFRelease(colors);
  24. CGColorSpaceRelease(colorSpace);
  25. }
  26. return gradient;
  27. }
  28.  
  29. - (void)drawRect:(CGRect)rect
  30. {
  31. CGContextRef context = UIGraphicsGetCurrentContext();
  32. CGContextSaveGState(context);
  33.  
  34. CGContextSetAllowsAntialiasing(context,YES);
  35. CGContextSetShouldAntialias(context,YES);
  36.  
  37. // Fill background color
  38. [[UIColor whiteColor] setFill];
  39. UIRectFill(rect);
  40.  
  41. // Build a path
  42. CGFloat strokeWidth = 10.f;
  43. CGContextSetLineWidth(context,strokeWidth);
  44.  
  45. CGGradientRef gradient = [self lineGradient];
  46.  
  47. CGPoint points[9] = {
  48. CGPointMake(10.f,25.f),CGPointMake(100.f,100.f),150.f),CGPointMake(22.f,300.f),CGPointMake(230.f,400.f),200.f),CGPointMake(300.f,CGPointMake(310.f,160.f),CGPointMake(280.f,100.f)
  49. };
  50.  
  51.  
  52. for (NSUInteger i = 1; i < 9; i++) {
  53. CGPoint start = points[i - 1];
  54. CGPoint end = points[i];
  55. CGFloat dy = end.y - start.y;
  56. CGFloat dx = end.x - start.x;
  57. CGFloat xOffset,yOffset;
  58. // Remember that,unlike Cartesian geometry,origin is in *upper* left!
  59. if (dx == 0) {
  60. // Vertical to start,gradient is horizontal
  61. xOffset = 0.5 * strokeWidth;
  62. yOffset = 0.f;
  63. if (dy < 0) {
  64. xOffset *= -1;
  65. }
  66. }
  67. else if (dy == 0) {
  68. // Horizontal to start,gradient is vertical
  69. xOffset = 0.f;
  70. yOffset = 0.5 * strokeWidth;
  71. }
  72. else {
  73. // Sloped
  74. CGFloat gradientSlope = - dx / dy;
  75. xOffset = 0.5 * strokeWidth / sqrt(1 + gradientSlope * gradientSlope);
  76. yOffset = 0.5 * strokeWidth / sqrt(1 + 1 / (gradientSlope * gradientSlope));
  77. if (dx < 0 && dy > 0) {
  78. yOffset *= -1;
  79. }
  80. else if (dx > 0 && dy < 0) {
  81. xOffset *= -1;
  82. }
  83. else if (dx < 0 && dy < 0) {
  84. yOffset *= -1;
  85. xOffset *= -1;
  86. }
  87. else {
  88. }
  89. }
  90. CGAffineTransform startTransform = CGAffineTransformMakeTranslation(-xOffset,yOffset);
  91. CGAffineTransform endTransform = CGAffineTransformMakeTranslation(xOffset,-yOffset);
  92. CGPoint gradientStart = CGPointApplyAffineTransform(start,startTransform);
  93. CGPoint gradientEnd = CGPointApplyAffineTransform(start,endTransform);
  94.  
  95. CGContextSaveGState(context);
  96. CGContextMoveToPoint(context,start.x,start.y);
  97. CGContextAddLineToPoint(context,end.x,end.y);
  98. CGContextReplacePathWithStrokedPath(context);
  99. CGContextClip(context);
  100. CGContextDrawLinearGradient(context,gradient,gradientStart,gradientEnd,kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation);
  101. CGContextRestoreGState(context);
  102. }
  103.  
  104. CGContextRestoreGState(context);
  105. }

猜你在找的iOS相关文章