QBPopupMenuPagenatorView.m 4.1 KB

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