123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // QBPopupMenuPagenatorView.m
- // QBPopupMenu
- //
- // Created by Tanaka Katsuma on 2013/11/23.
- // Copyright (c) 2013年 Katsuma Tanaka. All rights reserved.
- //
- #import "QBPopupMenuPagenatorView.h"
- @implementation QBPopupMenuPagenatorView
- + (CGFloat)pagenatorWidth
- {
- return 10 + 10 * 2;
- }
- + (instancetype)leftPagenatorViewWithTarget:(id)target action:(SEL)action
- {
- return [[self alloc] initWithArrowDirection:QBPopupMenuPagenatorDirectionLeft target:target action:action];
- }
- + (instancetype)rightPagenatorViewWithTarget:(id)target action:(SEL)action
- {
- return [[self alloc] initWithArrowDirection:QBPopupMenuPagenatorDirectionRight target:target action:action];
- }
- - (instancetype)initWithArrowDirection:(QBPopupMenuPagenatorDirection)arrowDirection target:(id)target action:(SEL)action
- {
- self = [super initWithItem:nil];
-
- if (self) {
- // Property settings
- self.target = target;
- self.action = action;
-
- // Set arrow image
- UIImage *arrowImage = [self arrowImageWithSize:CGSizeMake(10, 10)
- direction:arrowDirection
- highlighted:NO];
- [self.button setImage:arrowImage forState:UIControlStateNormal];
-
- UIImage *highlightedArrowImage = [self arrowImageWithSize:CGSizeMake(10, 10)
- direction:arrowDirection
- highlighted:YES];
- [self.button setImage:highlightedArrowImage forState:UIControlStateHighlighted];
- }
-
- return self;
- }
- #pragma mark - Actions
- - (void)performAction
- {
- if (self.target && self.action) {
- [self.target performSelector:self.action withObject:nil afterDelay:0];
- }
- }
- #pragma mark - Updating the View
- - (CGSize)sizeThatFits:(CGSize)size
- {
- CGSize buttonSize = [self.button sizeThatFits:CGSizeZero];
- buttonSize.width = [[self class] pagenatorWidth];
-
- return buttonSize;
- }
- - (UIImage *)arrowImageWithSize:(CGSize)size direction:(QBPopupMenuPagenatorDirection)direction highlighted:(BOOL)highlighted
- {
- UIGraphicsBeginImageContextWithOptions(size, NO, 0);
-
- // Draw arrow
- [self drawArrowInRect:CGRectMake(0, 0, size.width, size.height) direction:direction highlighted:highlighted];
-
- // Create image from buffer
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
-
- UIGraphicsEndImageContext();
-
- return image;
- }
- #pragma mark - Creating Paths
- - (CGMutablePathRef)arrowPathInRect:(CGRect)rect direction:(QBPopupMenuPagenatorDirection)direction
- {
- CGMutablePathRef path = CGPathCreateMutable();
-
- switch (direction) {
- case QBPopupMenuPagenatorDirectionLeft:
- {
- CGPathMoveToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
- CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
- CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height / 2.0);
- CGPathCloseSubpath(path);
- }
- break;
-
- case QBPopupMenuPagenatorDirectionRight:
- {
- CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
- CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
- CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height / 2.0);
- CGPathCloseSubpath(path);
- }
- break;
-
- default:
- break;
- }
-
- return path;
- }
- #pragma mark - Drawing
- - (void)drawArrowInRect:(CGRect)rect direction:(QBPopupMenuPagenatorDirection)direction highlighted:(BOOL)highlighted
- {
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSaveGState(context);
-
- CGMutablePathRef path = [self arrowPathInRect:rect direction:direction];
- CGContextAddPath(context, path);
-
- CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
- CGContextFillPath(context);
-
- CGPathRelease(path);
-
- CGContextRestoreGState(context);
- }
- @end
|