ChatBubbleLayer.m 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // ChatBubbleLayer.m
  3. // ChatImageBubbleDemo
  4. //
  5. // Created by HuminiOS on 15/10/28.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "ChatBubbleLayer.h"
  9. #define BubbleWidth self.bounds.size.width
  10. #define BubbleHeight self.bounds.size.height
  11. @implementation ChatBubbleLayer
  12. - (void)drawInContext:(CGContextRef)ctx {
  13. if (_isReceivedBubble) {
  14. // C----D -
  15. // | | | -> triangleY
  16. // | | |
  17. // < | -
  18. // | |
  19. // | |
  20. // B----A 四个点
  21. //
  22. UIBezierPath *bubblePath = [UIBezierPath bezierPath];
  23. // point A
  24. [bubblePath moveToPoint:CGPointMake(BubbleWidth , BubbleHeight - cornerRadiuslength)];
  25. [bubblePath addQuadCurveToPoint:CGPointMake(BubbleWidth - cornerRadiuslength, BubbleHeight) controlPoint:CGPointMake(BubbleWidth - crossgrap, BubbleHeight)];
  26. // point B
  27. [bubblePath addLineToPoint:CGPointMake(cornerRadiuslength + crossgrap, BubbleHeight)];
  28. [bubblePath addQuadCurveToPoint:CGPointMake(crossgrap, BubbleHeight - cornerRadiuslength) controlPoint:CGPointMake(crossgrap, BubbleHeight)];
  29. // point C
  30. [bubblePath addLineToPoint:CGPointMake(crossgrap, cornerRadiuslength)];
  31. [bubblePath addQuadCurveToPoint:CGPointMake(cornerRadiuslength + crossgrap, 0) controlPoint:CGPointMake(crossgrap, 0)];
  32. // point D
  33. [bubblePath addLineToPoint:CGPointMake(BubbleWidth - cornerRadiuslength, 0)];
  34. [bubblePath addQuadCurveToPoint:CGPointMake(BubbleWidth, cornerRadiuslength) controlPoint:CGPointMake(BubbleWidth, 0)];
  35. [bubblePath closePath];
  36. UIBezierPath *trianglePath = [UIBezierPath bezierPath];
  37. [trianglePath moveToPoint:CGPointMake(crossgrap, triangleY)];
  38. [trianglePath addLineToPoint:CGPointMake(0, triangleY + 6)];
  39. [trianglePath addLineToPoint:CGPointMake(crossgrap, triangleY + 12)];
  40. [trianglePath closePath];
  41. CGContextAddPath(ctx, trianglePath.CGPath);
  42. CGContextAddPath(ctx, bubblePath.CGPath);
  43. CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:29.0/255.0 green:163.0/255.0 blue:1 alpha:1].CGColor);
  44. CGContextFillPath(ctx);
  45. } else {
  46. // C----D -
  47. // | | | -> triangleY
  48. // | | |
  49. // | > -
  50. // | |
  51. // | |
  52. // B----A 四个点
  53. //
  54. UIBezierPath *bubblePath = [UIBezierPath bezierPath];
  55. // point A
  56. [bubblePath moveToPoint:CGPointMake(BubbleWidth - crossgrap , BubbleHeight - cornerRadiuslength)];
  57. [bubblePath addQuadCurveToPoint:CGPointMake(BubbleWidth - crossgrap - cornerRadiuslength, BubbleHeight) controlPoint:CGPointMake(BubbleWidth - crossgrap, BubbleHeight)];
  58. // point B
  59. [bubblePath addLineToPoint:CGPointMake(cornerRadiuslength, BubbleHeight)];
  60. [bubblePath addQuadCurveToPoint:CGPointMake(0, BubbleHeight - cornerRadiuslength) controlPoint:CGPointMake(0, BubbleHeight)];
  61. // point C
  62. [bubblePath addLineToPoint:CGPointMake(0, cornerRadiuslength)];
  63. [bubblePath addQuadCurveToPoint:CGPointMake(cornerRadiuslength, 0) controlPoint:CGPointMake(0, 0)];
  64. // point D
  65. [bubblePath addLineToPoint:CGPointMake(BubbleWidth - crossgrap - cornerRadiuslength, 0)];
  66. [bubblePath addQuadCurveToPoint:CGPointMake(BubbleWidth - crossgrap, cornerRadiuslength) controlPoint:CGPointMake(BubbleWidth - crossgrap, 0)];
  67. [bubblePath closePath];
  68. UIBezierPath *trianglePath = [UIBezierPath bezierPath];
  69. [trianglePath moveToPoint:CGPointMake(BubbleWidth - crossgrap, triangleY)];
  70. [trianglePath addLineToPoint:CGPointMake(BubbleWidth, triangleY + 6)];
  71. [trianglePath addLineToPoint:CGPointMake(BubbleWidth - crossgrap, triangleY + 12)];
  72. [trianglePath closePath];
  73. CGContextAddPath(ctx, trianglePath.CGPath);
  74. CGContextAddPath(ctx, bubblePath.CGPath);
  75. CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:29.0/255.0 green:163.0/255.0 blue:1 alpha:1].CGColor);
  76. CGContextFillPath(ctx);
  77. }
  78. }
  79. @end