TCPageIndicator.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // TCPageIndicator.m
  3. // MyMacDevDemo
  4. //
  5. // Created by tangchao on 2021/12/11.
  6. //
  7. #import "TCPageIndicator.h"
  8. @interface TCPageIndicator () {}
  9. @property (nonatomic, strong) NSMutableArray *indicatorRects;
  10. @end
  11. @implementation TCPageIndicator
  12. - (instancetype)initWithFrame:(NSRect)frameRect {
  13. if (self = [super initWithFrame:frameRect]) {
  14. self.selectedColor = [NSColor grayColor];
  15. self.normalColor = [[NSColor grayColor] colorWithAlphaComponent:0.5];
  16. self.indicatorMargin = 8.f;
  17. self.currentPage = 0;
  18. self.numberOfPages = 0;
  19. self.pageIndicatorSize = CGSizeMake(6, 6);
  20. self.enabled = YES;
  21. self.indicatorRects = [NSMutableArray arrayWithCapacity:4];
  22. }
  23. return self;
  24. }
  25. - (void)setIndicatorMargin:(CGFloat)indicatorMargin {
  26. _indicatorMargin = indicatorMargin;
  27. [self setNeedsDisplay:YES];
  28. }
  29. - (void)setCurrentPage:(NSUInteger)currentPage {
  30. _currentPage = currentPage;
  31. [self setNeedsDisplay:YES];
  32. }
  33. - (void)setNumberOfPages:(NSUInteger)numberOfPages {
  34. _numberOfPages = numberOfPages;
  35. [self setNeedsDisplay:YES];
  36. }
  37. - (void)setPageIndicatorSize:(NSSize)pageIndicatorSize {
  38. _pageIndicatorSize = pageIndicatorSize;
  39. [self setNeedsDisplay:YES];
  40. }
  41. - (void)drawRect:(NSRect)dirtyRect {
  42. [super drawRect:dirtyRect];
  43. CGFloat indicatorAreaWidth = self.pageIndicatorSize.width * self.numberOfPages + self.indicatorMargin * (self.numberOfPages-1);
  44. CGFloat leftPosition = (self.bounds.size.width - indicatorAreaWidth) * 0.5;
  45. CGFloat topPadding = (self.bounds.size.height - self.pageIndicatorSize.height) * 0.5;
  46. [self.indicatorRects removeAllObjects];
  47. for (int i = 0; i < self.numberOfPages; i++) {
  48. NSPoint position = NSMakePoint(leftPosition, topPadding);
  49. CGRect rect = CGRectMake(position.x, position.y, self.pageIndicatorSize.width, self.pageIndicatorSize.height);
  50. [self.indicatorRects addObject:[NSValue valueWithRect:rect]];
  51. NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:rect];
  52. if (self.currentPage == i) {
  53. [self.selectedColor setFill];
  54. } else {
  55. [self.normalColor setFill];
  56. }
  57. [path fill];
  58. leftPosition += (self.pageIndicatorSize.width + self.indicatorMargin);
  59. }
  60. }
  61. - (void)mouseDown:(NSEvent *)event {
  62. [super mouseDown:event];
  63. if (self.enabled == NO) {
  64. return;
  65. }
  66. NSPoint eventLocation = event.locationInWindow;
  67. /// 转换成视图的本地坐标
  68. NSPoint pointInView = [self convertPoint:eventLocation fromView:nil];
  69. for (int i = 0; i < self.numberOfPages; i++) {
  70. NSRect rect = CGRectInset([self.indicatorRects[i] CGRectValue], -2, -2);
  71. if (NSPointInRect(pointInView, rect)) {
  72. self.currentPage = i;
  73. }
  74. }
  75. }
  76. @end