QBPopupMenuPagenatorView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // QBPopupMenuPagenatorView.m
  3. // QBPopupMenu
  4. //
  5. // Created by Tanaka Katsuma on 2013/11/23.
  6. // Copyright (c) 2013年 Katsuma Tanaka. All rights reserved.
  7. //
  8. #import "QBPopupMenuPagenatorView.h"
  9. @implementation QBPopupMenuPagenatorView
  10. + (CGFloat)pagenatorWidth
  11. {
  12. return 10 + 10 * 2;
  13. }
  14. /* Custom */
  15. + (instancetype)leftPagenatorViewWithTarget:(id)target action:(SEL)action accessibilityLabel:(NSString *)accessibilityLabel
  16. {
  17. return [[self alloc] initWithArrowDirection:QBPopupMenuPagenatorDirectionLeft target:target action:action accessibilityLabel:accessibilityLabel];
  18. }
  19. /* Custom */
  20. + (instancetype)rightPagenatorViewWithTarget:(id)target action:(SEL)action accessibilityLabel:(NSString *)accessibilityLabel
  21. {
  22. return [[self alloc] initWithArrowDirection:QBPopupMenuPagenatorDirectionRight target:target action:action accessibilityLabel:(NSString *)accessibilityLabel];
  23. }
  24. /* Custom */
  25. - (instancetype)initWithArrowDirection:(QBPopupMenuPagenatorDirection)arrowDirection target:(id)target action:(SEL)action accessibilityLabel:(NSString *)accessibilityLabel
  26. {
  27. self = [super initWithItem:nil];
  28. if (self) {
  29. // Property settings
  30. self.target = target;
  31. self.action = action;
  32. /* Custom */
  33. self.accessibilityLabel = accessibilityLabel;
  34. // Set arrow image
  35. UIImage *arrowImage = [self arrowImageWithSize:CGSizeMake(10, 10)
  36. direction:arrowDirection
  37. highlighted:NO];
  38. [self.button setImage:arrowImage forState:UIControlStateNormal];
  39. UIImage *highlightedArrowImage = [self arrowImageWithSize:CGSizeMake(10, 10)
  40. direction:arrowDirection
  41. highlighted:YES];
  42. [self.button setImage:highlightedArrowImage forState:UIControlStateHighlighted];
  43. }
  44. return self;
  45. }
  46. #pragma mark - Actions
  47. - (void)performAction
  48. {
  49. if (self.target && self.action) {
  50. [self.target performSelector:self.action withObject:nil afterDelay:0];
  51. }
  52. }
  53. #pragma mark - Updating the View
  54. - (CGSize)sizeThatFits:(CGSize)size
  55. {
  56. CGSize buttonSize = [self.button sizeThatFits:CGSizeZero];
  57. buttonSize.width = [[self class] pagenatorWidth];
  58. return buttonSize;
  59. }
  60. - (UIImage *)arrowImageWithSize:(CGSize)size direction:(QBPopupMenuPagenatorDirection)direction highlighted:(BOOL)highlighted
  61. {
  62. UIGraphicsBeginImageContextWithOptions(size, NO, 0);
  63. // Draw arrow
  64. [self drawArrowInRect:CGRectMake(0, 0, size.width, size.height) direction:direction highlighted:highlighted];
  65. // Create image from buffer
  66. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  67. UIGraphicsEndImageContext();
  68. return image;
  69. }
  70. #pragma mark - Creating Paths
  71. - (CGMutablePathRef)arrowPathInRect:(CGRect)rect direction:(QBPopupMenuPagenatorDirection)direction
  72. {
  73. CGMutablePathRef path = CGPathCreateMutable();
  74. switch (direction) {
  75. case QBPopupMenuPagenatorDirectionLeft:
  76. {
  77. CGPathMoveToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
  78. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
  79. CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height / 2.0);
  80. CGPathCloseSubpath(path);
  81. }
  82. break;
  83. case QBPopupMenuPagenatorDirectionRight:
  84. {
  85. CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
  86. CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
  87. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height / 2.0);
  88. CGPathCloseSubpath(path);
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. return path;
  95. }
  96. #pragma mark - Drawing
  97. - (void)drawArrowInRect:(CGRect)rect direction:(QBPopupMenuPagenatorDirection)direction highlighted:(BOOL)highlighted
  98. {
  99. CGContextRef context = UIGraphicsGetCurrentContext();
  100. CGContextSaveGState(context);
  101. CGMutablePathRef path = [self arrowPathInRect:rect direction:direction];
  102. CGContextAddPath(context, path);
  103. CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
  104. CGContextFillPath(context);
  105. CGPathRelease(path);
  106. CGContextRestoreGState(context);
  107. }
  108. @end