MODashedLineView.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // MODashedLineView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/3/3.
  6. //
  7. #import "MODashedLineView.h"
  8. @implementation MODashedLineView
  9. - (instancetype)initWithFrame:(CGRect)frame {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. [self commonInit];
  13. }
  14. return self;
  15. }
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. [self commonInit];
  19. }
  20. - (void)prepareForInterfaceBuilder {
  21. [super prepareForInterfaceBuilder];
  22. [self setNeedsDisplay];
  23. }
  24. - (void)commonInit {
  25. self.backgroundColor = [UIColor clearColor];
  26. self.dashColor = self.dashColor ?: [UIColor blackColor];
  27. self.dashLength = self.dashLength ?: 5;
  28. self.dashSpacing = self.dashSpacing ?: 5;
  29. self.lineWidth = self.lineWidth ?: 2;
  30. }
  31. - (void)drawRect:(CGRect)rect {
  32. CGContextRef context = UIGraphicsGetCurrentContext();
  33. CGContextSetStrokeColorWithColor(context, self.dashColor.CGColor);
  34. CGContextSetLineWidth(context, self.lineWidth);
  35. CGFloat dashPattern[] = {self.dashLength, self.dashSpacing};
  36. CGContextSetLineDash(context, 0, dashPattern, 2);
  37. CGContextMoveToPoint(context, 0, rect.size.height / 2);
  38. CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
  39. CGContextStrokePath(context);
  40. }
  41. @end