123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- //
- // YBPopupMenu.m
- // YBPopupMenu
- //
- // Created by lyb on 2017/5/10.
- // Copyright © 2017年 lyb. All rights reserved.
- //
- #import "YBPopupMenu.h"
- #import "YBPopupMenuPath.h"
- #define YBScreenWidth [UIScreen mainScreen].bounds.size.width
- #define YBScreenHeight [UIScreen mainScreen].bounds.size.height
- #define YBMainWindow [UIApplication sharedApplication].keyWindow
- #define YB_SAFE_BLOCK(BlockName, ...) ({ !BlockName ? nil : BlockName(__VA_ARGS__); })
- #pragma mark - /////////////
- #pragma mark - private cell
- @interface YBPopupMenuCell : UITableViewCell
- @property (nonatomic, assign) BOOL isShowSeparator;
- @property (nonatomic, strong) UIColor * separatorColor;
- @end
- @implementation YBPopupMenuCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- _isShowSeparator = NO;
- _separatorColor = [UIColor lightGrayColor];
- [self setNeedsDisplay];
- }
- return self;
- }
- - (void)setIsShowSeparator:(BOOL)isShowSeparator
- {
- _isShowSeparator = isShowSeparator;
- [self setNeedsDisplay];
- }
- - (void)setSeparatorColor:(UIColor *)separatorColor
- {
- _separatorColor = separatorColor;
- [self setNeedsDisplay];
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- self.contentView.frame = self.bounds;
- self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- }
- - (void)drawRect:(CGRect)rect
- {
- if (!_isShowSeparator) return;
- UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, rect.size.height - 0.5, rect.size.width, 0.5)];
- [_separatorColor setFill];
- [bezierPath fillWithBlendMode:kCGBlendModeNormal alpha:1];
- [bezierPath closePath];
- }
- @end
- @interface YBPopupMenu ()
- <
- UITableViewDelegate,
- UITableViewDataSource
- >
- @property (nonatomic, strong) UIView * menuBackView;
- @property (nonatomic) CGRect relyRect;
- @property (nonatomic, assign) CGFloat itemWidth;
- @property (nonatomic) CGPoint point;
- @property (nonatomic, assign) BOOL isCornerChanged;
- @property (nonatomic, strong) UIColor * separatorColor;
- @property (nonatomic, assign) BOOL isChangeDirection;
- @property (nonatomic, strong) UIView * relyView;
- @end
- @implementation YBPopupMenu
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- [self setDefaultSettings];
- }
- return self;
- }
- #pragma mark - publics
- + (YBPopupMenu *)showAtPoint:(CGPoint)point titles:(NSArray *)titles icons:(NSArray *)icons menuWidth:(CGFloat)itemWidth otherSettings:(void (^) (YBPopupMenu * popupMenu))otherSetting
- {
- YBPopupMenu *popupMenu = [[YBPopupMenu alloc] init];
- popupMenu.point = point;
- popupMenu.titles = titles;
- popupMenu.images = icons;
- popupMenu.itemWidth = itemWidth;
- YB_SAFE_BLOCK(otherSetting,popupMenu);
- [popupMenu show];
- return popupMenu;
- }
- + (YBPopupMenu *)showRelyOnView:(UIView *)view titles:(NSArray *)titles icons:(NSArray *)icons menuWidth:(CGFloat)itemWidth otherSettings:(void (^) (YBPopupMenu * popupMenu))otherSetting
- {
- YBPopupMenu *popupMenu = [[YBPopupMenu alloc] init];
- popupMenu.relyView = view;
- popupMenu.titles = titles;
- popupMenu.images = icons;
- popupMenu.itemWidth = itemWidth;
- YB_SAFE_BLOCK(otherSetting,popupMenu);
- [popupMenu show];
- return popupMenu;
- }
- - (void)dismiss
- {
- [self.orientationManager endMonitorDeviceOrientation];
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenuBeganDismiss:)]) {
- [self.delegate ybPopupMenuBeganDismiss:self];
- }
- __weak typeof(self) weakSelf = self;
- [self.animationManager displayDismissAnimationCompletion:^{
- __strong typeof(weakSelf)self = weakSelf;
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenuDidDismiss:)]) {
- [self.delegate ybPopupMenuDidDismiss:self];
- }
- self.delegate = nil;
- [self removeFromSuperview];
- [self.menuBackView removeFromSuperview];
- }];
- }
- + (void)dismissAllPopupMenu
- {
- for (UIView * subView in YBMainWindow.subviews) {
- if ([subView isKindOfClass:[YBPopupMenu class]]) {
- YBPopupMenu * popupMenu = (YBPopupMenu *)subView;
- [popupMenu dismiss];
- }
- }
- }
- #pragma mark tableViewDelegate & dataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _titles.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell * tableViewCell = nil;
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenu:cellForRowAtIndex:)]) {
- tableViewCell = [self.delegate ybPopupMenu:self cellForRowAtIndex:indexPath.row];
- }
-
- if (tableViewCell) {
- return tableViewCell;
- }
-
- static NSString * identifier = @"ybPopupMenu";
- YBPopupMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (!cell) {
- cell = [[YBPopupMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
- cell.textLabel.numberOfLines = 0;
- }
- cell.backgroundColor = [UIColor clearColor];
- cell.textLabel.textColor = _textColor;
- if (_font) {
- cell.textLabel.font = _font;
- }else {
- cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
- }
- if (self.isCenter) {
- cell.textLabel.textAlignment = NSTextAlignmentCenter;
- }
- if ([_titles[indexPath.row] isKindOfClass:[NSAttributedString class]]) {
- cell.textLabel.attributedText = _titles[indexPath.row];
- }else if ([_titles[indexPath.row] isKindOfClass:[NSString class]]) {
- cell.textLabel.text = _titles[indexPath.row];
- }else {
- cell.textLabel.text = nil;
- }
- cell.separatorColor = _separatorColor;
- if (_images.count >= indexPath.row + 1) {
- if ([_images[indexPath.row] isKindOfClass:[NSString class]]) {
- cell.imageView.image = [UIImage imageNamed:_images[indexPath.row]];
- }else if ([_images[indexPath.row] isKindOfClass:[UIImage class]]){
- cell.imageView.image = _images[indexPath.row];
- }else {
- cell.imageView.image = nil;
- }
- }else {
- cell.imageView.image = nil;
- }
- [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenu:heightForRowAtIndex:)]) {
- return [self.delegate ybPopupMenu:self heightForRowAtIndex:indexPath.row];
- }
- return _itemHeight;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- if (_dismissOnSelected) [self dismiss];
- if (self.selectTextColor) {
- YBPopupMenuCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- cell.textLabel.textColor = HexColorFromRGB(0xFF0084);
- }
-
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenu:didSelectedAtIndex:)]) {
- [self.delegate ybPopupMenu:self didSelectedAtIndex:indexPath.row];
- }
- }
- #pragma mark - scrollViewDelegate
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- if ([[self getLastVisibleCell] isKindOfClass:[YBPopupMenuCell class]]) {
- YBPopupMenuCell *cell = [self getLastVisibleCell];
- cell.isShowSeparator = NO;
- }
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- if ([[self getLastVisibleCell] isKindOfClass:[YBPopupMenuCell class]]) {
- YBPopupMenuCell *cell = [self getLastVisibleCell];
- cell.isShowSeparator = NO;
- }
- }
- - (YBPopupMenuCell *)getLastVisibleCell
- {
- NSArray <NSIndexPath *>*indexPaths = [self.tableView indexPathsForVisibleRows];
- indexPaths = [indexPaths sortedArrayUsingComparator:^NSComparisonResult(NSIndexPath * _Nonnull obj1, NSIndexPath * _Nonnull obj2) {
- return obj1.row < obj2.row;
- }];
- NSIndexPath *indexPath = indexPaths.firstObject;
- return [self.tableView cellForRowAtIndexPath:indexPath];
- }
- #pragma mark - privates
- - (void)show
- {
- [self.orientationManager startMonitorDeviceOrientation];
- [self updateUI];
- [YBMainWindow addSubview:_menuBackView];
- [YBMainWindow addSubview:self];
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenuBeganShow:)]) {
- [self.delegate ybPopupMenuBeganShow:self];
- }
- if ([[self getLastVisibleCell] isKindOfClass:[YBPopupMenuCell class]]) {
- YBPopupMenuCell *cell = [self getLastVisibleCell];
- cell.isShowSeparator = NO;
- }
- __weak typeof(self) weakSelf = self;
- [self.animationManager displayShowAnimationCompletion:^{
- __strong typeof(weakSelf)self = weakSelf;
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(ybPopupMenuDidShow:)]) {
- [self.delegate ybPopupMenuDidShow:self];
- }
- }];
- }
- - (void)setDefaultSettings
- {
- _cornerRadius = 5.0;
- _rectCorner = UIRectCornerAllCorners;
- self.isShowShadow = YES;
- _dismissOnSelected = YES;
- _dismissOnTouchOutside = YES;
- _fontSize = 15;
- _textColor = [UIColor blackColor];
- _offset = 0.0;
- _relyRect = CGRectZero;
- _point = CGPointZero;
- _borderWidth = 0.0;
- _borderColor = [UIColor lightGrayColor];
- _arrowWidth = 15.0;
- _arrowHeight = 10.0;
- _backColor = [UIColor whiteColor];
- _type = YBPopupMenuTypeDefault;
- _arrowDirection = YBPopupMenuArrowDirectionTop;
- _priorityDirection = YBPopupMenuPriorityDirectionTop;
- _minSpace = 10.0;
- _maxVisibleCount = 5;
- _itemHeight = 44;
- _isCornerChanged = NO;
- _showMaskView = YES;
- _orientationManager = [YBPopupMenuDeviceOrientationManager manager];
- _animationManager = [YBPopupMenuAnimationManager manager];
- _animationManager.animationView = self;
- _menuBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YBScreenWidth, YBScreenHeight)];
- _menuBackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
- _menuBackView.alpha = 1;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(touchOutSide)];
- [_menuBackView addGestureRecognizer: tap];
- self.alpha = 1;
- self.backgroundColor = [UIColor clearColor];
- [self addSubview:self.tableView];
-
- __weak typeof(self) weakSelf = self;
- [_orientationManager setDeviceOrientDidChangeHandle:^(UIInterfaceOrientation orientation) {
- __strong typeof(weakSelf)self = weakSelf;
- if (orientation == UIInterfaceOrientationPortrait ||
- orientation == UIInterfaceOrientationLandscapeLeft ||
- orientation == UIInterfaceOrientationLandscapeRight)
- {
- if (self.relyView) {
- //依赖view
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- //需要延迟加载才可以获取真实的frame,这里先做个标记,若有更合适的方法再替换
- [self calculateRealPointIfNeed];
- [self updateUI];
- });
- }else {
- //依赖point
- [self updateUI];
- }
- }
- }];
- }
- - (UITableView *)tableView
- {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.backgroundColor = [UIColor clearColor];
- _tableView.tableFooterView = [UIView new];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- }
- return _tableView;
- }
- - (void)touchOutSide
- {
- if (_dismissOnTouchOutside) {
- [self dismiss];
- }
- }
- - (void)setIsShowShadow:(BOOL)isShowShadow
- {
- _isShowShadow = isShowShadow;
- self.layer.shadowOpacity = isShowShadow ? 0.5 : 0;
- self.layer.shadowOffset = CGSizeMake(0, 0);
- self.layer.shadowRadius = isShowShadow ? 2.0 : 0;
- }
- - (void)setRelyView:(UIView *)relyView
- {
- _relyView = relyView;
- [self calculateRealPointIfNeed];
- }
- - (void)calculateRealPointIfNeed
- {
- CGRect absoluteRect = [_relyView convertRect:_relyView.bounds toView:YBMainWindow];
- CGPoint relyPoint = CGPointMake(absoluteRect.origin.x + absoluteRect.size.width / 2, absoluteRect.origin.y + absoluteRect.size.height);
- self.relyRect = absoluteRect;
- self.point = relyPoint;
- }
- - (void)setShowMaskView:(BOOL)showMaskView
- {
- _showMaskView = showMaskView;
- _menuBackView.backgroundColor = showMaskView ? [[UIColor blackColor] colorWithAlphaComponent:0.1] : [UIColor clearColor];
- }
- - (void)setType:(YBPopupMenuType)type
- {
- _type = type;
- switch (type) {
- case YBPopupMenuTypeDark:
- {
- _textColor = [UIColor lightGrayColor];
- _backColor = [UIColor colorWithRed:0.25 green:0.27 blue:0.29 alpha:1];
- _separatorColor = [UIColor lightGrayColor];
- }
- break;
-
- default:
- {
- _textColor = [UIColor blackColor];
- _backColor = [UIColor whiteColor];
- _separatorColor = [UIColor lightGrayColor];
- }
- break;
- }
- }
- - (void)setTitles:(NSArray *)titles
- {
- _titles = titles;
- }
- - (void)setImages:(NSArray *)images
- {
- _images = images;
- }
- - (void)updateUI
- {
- _menuBackView.frame = CGRectMake(0, 0, YBScreenWidth, YBScreenHeight);
- CGFloat height;
- if (_titles.count > _maxVisibleCount) {
- height = _itemHeight * _maxVisibleCount + _borderWidth * 2;
- self.tableView.bounces = YES;
- }else {
- height = _itemHeight * _titles.count + _borderWidth * 2;
- self.tableView.bounces = NO;
- }
- _isChangeDirection = NO;
- if (_priorityDirection == YBPopupMenuPriorityDirectionTop) {
- if (_point.y + height + _arrowHeight > YBScreenHeight - _minSpace) {
- _arrowDirection = YBPopupMenuArrowDirectionBottom;
- _isChangeDirection = YES;
- }else {
- _arrowDirection = YBPopupMenuArrowDirectionTop;
- _isChangeDirection = NO;
- }
- }else if (_priorityDirection == YBPopupMenuPriorityDirectionBottom) {
- if (_point.y - height - _arrowHeight < _minSpace) {
- _arrowDirection = YBPopupMenuArrowDirectionTop;
- _isChangeDirection = YES;
- }else {
- _arrowDirection = YBPopupMenuArrowDirectionBottom;
- _isChangeDirection = NO;
- }
- }else if (_priorityDirection == YBPopupMenuPriorityDirectionLeft) {
- if (_point.x + _itemWidth + _arrowHeight > YBScreenWidth - _minSpace) {
- _arrowDirection = YBPopupMenuArrowDirectionRight;
- _isChangeDirection = YES;
- }else {
- _arrowDirection = YBPopupMenuArrowDirectionLeft;
- _isChangeDirection = NO;
- }
- }else if (_priorityDirection == YBPopupMenuPriorityDirectionRight) {
- if (_point.x - _itemWidth - _arrowHeight < _minSpace) {
- _arrowDirection = YBPopupMenuArrowDirectionLeft;
- _isChangeDirection = YES;
- }else {
- _arrowDirection = YBPopupMenuArrowDirectionRight;
- _isChangeDirection = NO;
- }
- }
- [self setArrowPosition];
- [self setRelyRect];
- if (_arrowDirection == YBPopupMenuArrowDirectionTop) {
- CGFloat y = _isChangeDirection ? _point.y : _point.y;
- if (_arrowPosition > _itemWidth / 2) {
- self.frame = CGRectMake(YBScreenWidth - _minSpace - _itemWidth, y, _itemWidth, height + _arrowHeight);
- }else if (_arrowPosition < _itemWidth / 2) {
- self.frame = CGRectMake(_minSpace, y, _itemWidth, height + _arrowHeight);
- }else {
- self.frame = CGRectMake(_point.x - _itemWidth / 2, y, _itemWidth, height + _arrowHeight);
- }
- }else if (_arrowDirection == YBPopupMenuArrowDirectionBottom) {
- CGFloat y = _isChangeDirection ? _point.y - _arrowHeight - height : _point.y - _arrowHeight - height;
- if (_arrowPosition > _itemWidth / 2) {
- self.frame = CGRectMake(YBScreenWidth - _minSpace - _itemWidth, y, _itemWidth, height + _arrowHeight);
- }else if (_arrowPosition < _itemWidth / 2) {
- self.frame = CGRectMake(_minSpace, y, _itemWidth, height + _arrowHeight);
- }else {
- self.frame = CGRectMake(_point.x - _itemWidth / 2, y, _itemWidth, height + _arrowHeight);
- }
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft) {
- CGFloat x = _isChangeDirection ? _point.x : _point.x;
- if (_arrowPosition < _itemHeight / 2) {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }else if (_arrowPosition > _itemHeight / 2) {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }else {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }
- }else if (_arrowDirection == YBPopupMenuArrowDirectionRight) {
- CGFloat x = _isChangeDirection ? _point.x - _itemWidth - _arrowHeight : _point.x - _itemWidth - _arrowHeight;
- if (_arrowPosition < _itemHeight / 2) {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }else if (_arrowPosition > _itemHeight / 2) {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }else {
- self.frame = CGRectMake(x, _point.y - _arrowPosition, _itemWidth + _arrowHeight, height);
- }
- }else if (_arrowDirection == YBPopupMenuArrowDirectionNone) {
-
- }
-
- if (_isChangeDirection) {
- [self changeRectCorner];
- }
- [self setAnchorPoint];
- [self setOffset];
- [self.tableView reloadData];
- [self setNeedsDisplay];
- }
- - (void)setRelyRect
- {
- if (CGRectEqualToRect(_relyRect, CGRectZero)) {
- return;
- }
- if (_arrowDirection == YBPopupMenuArrowDirectionTop) {
- _point.y = _relyRect.size.height + _relyRect.origin.y;
- }else if (_arrowDirection == YBPopupMenuArrowDirectionBottom) {
- _point.y = _relyRect.origin.y;
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft) {
- _point = CGPointMake(_relyRect.origin.x + _relyRect.size.width, _relyRect.origin.y + _relyRect.size.height / 2);
- }else {
- _point = CGPointMake(_relyRect.origin.x, _relyRect.origin.y + _relyRect.size.height / 2);
- }
- }
- - (void)setFrame:(CGRect)frame
- {
- [super setFrame:frame];
- if (_arrowDirection == YBPopupMenuArrowDirectionTop) {
- self.tableView.frame = CGRectMake(_borderWidth, _borderWidth + _arrowHeight, frame.size.width - _borderWidth * 2, frame.size.height - _arrowHeight);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionBottom) {
- self.tableView.frame = CGRectMake(_borderWidth, _borderWidth, frame.size.width - _borderWidth * 2, frame.size.height - _arrowHeight);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft) {
- self.tableView.frame = CGRectMake(_borderWidth + _arrowHeight, _borderWidth , frame.size.width - _borderWidth * 2 - _arrowHeight, frame.size.height);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionRight) {
- self.tableView.frame = CGRectMake(_borderWidth , _borderWidth , frame.size.width - _borderWidth * 2 - _arrowHeight, frame.size.height);
- }
- }
- - (void)changeRectCorner
- {
- if (_isCornerChanged || _rectCorner == UIRectCornerAllCorners) {
- return;
- }
- BOOL haveTopLeftCorner = NO, haveTopRightCorner = NO, haveBottomLeftCorner = NO, haveBottomRightCorner = NO;
- if (_rectCorner & UIRectCornerTopLeft) {
- haveTopLeftCorner = YES;
- }
- if (_rectCorner & UIRectCornerTopRight) {
- haveTopRightCorner = YES;
- }
- if (_rectCorner & UIRectCornerBottomLeft) {
- haveBottomLeftCorner = YES;
- }
- if (_rectCorner & UIRectCornerBottomRight) {
- haveBottomRightCorner = YES;
- }
-
- if (_arrowDirection == YBPopupMenuArrowDirectionTop || _arrowDirection == YBPopupMenuArrowDirectionBottom) {
-
- if (haveTopLeftCorner) {
- _rectCorner = _rectCorner | UIRectCornerBottomLeft;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerBottomLeft);
- }
- if (haveTopRightCorner) {
- _rectCorner = _rectCorner | UIRectCornerBottomRight;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerBottomRight);
- }
- if (haveBottomLeftCorner) {
- _rectCorner = _rectCorner | UIRectCornerTopLeft;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerTopLeft);
- }
- if (haveBottomRightCorner) {
- _rectCorner = _rectCorner | UIRectCornerTopRight;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerTopRight);
- }
-
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft || _arrowDirection == YBPopupMenuArrowDirectionRight) {
- if (haveTopLeftCorner) {
- _rectCorner = _rectCorner | UIRectCornerTopRight;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerTopRight);
- }
- if (haveTopRightCorner) {
- _rectCorner = _rectCorner | UIRectCornerTopLeft;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerTopLeft);
- }
- if (haveBottomLeftCorner) {
- _rectCorner = _rectCorner | UIRectCornerBottomRight;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerBottomRight);
- }
- if (haveBottomRightCorner) {
- _rectCorner = _rectCorner | UIRectCornerBottomLeft;
- }else {
- _rectCorner = _rectCorner & (~UIRectCornerBottomLeft);
- }
- }
-
- _isCornerChanged = YES;
- }
- - (void)setOffset
- {
- if (_itemWidth == 0) return;
-
- CGRect originRect = self.frame;
-
- if (_arrowDirection == YBPopupMenuArrowDirectionTop) {
- originRect.origin.y += _offset;
- }else if (_arrowDirection == YBPopupMenuArrowDirectionBottom) {
- originRect.origin.y -= _offset;
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft) {
- originRect.origin.x += _offset;
- }else if (_arrowDirection == YBPopupMenuArrowDirectionRight) {
- originRect.origin.x -= _offset;
- }
- self.frame = originRect;
- }
- - (void)setAnchorPoint
- {
- if (_itemWidth == 0) return;
-
- CGFloat menuHeight = [self getMenuTotalHeight];
-
- CGPoint point = CGPointMake(0.5, 0.5);
- if (_arrowDirection == YBPopupMenuArrowDirectionTop) {
- point = CGPointMake(_arrowPosition / _itemWidth, 0);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionBottom) {
- point = CGPointMake(_arrowPosition / _itemWidth, 1);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft) {
- point = CGPointMake(0, _arrowPosition / menuHeight);
- }else if (_arrowDirection == YBPopupMenuArrowDirectionRight) {
- point = CGPointMake(1, _arrowPosition / menuHeight);
- }
- CGRect originRect = self.frame;
- self.layer.anchorPoint = point;
- self.frame = originRect;
- }
- - (void)setArrowPosition
- {
- if (_priorityDirection == YBPopupMenuPriorityDirectionNone) {
- return;
- }
-
- if (_arrowDirection == YBPopupMenuArrowDirectionTop || _arrowDirection == YBPopupMenuArrowDirectionBottom) {
- if (_point.x + _itemWidth / 2 > YBScreenWidth - _minSpace) {
- _arrowPosition = _itemWidth - (YBScreenWidth - _minSpace - _point.x);
- }else if (_point.x < _itemWidth / 2 + _minSpace) {
- _arrowPosition = _point.x - _minSpace;
- }else {
- _arrowPosition = _itemWidth / 2;
- }
-
- }else if (_arrowDirection == YBPopupMenuArrowDirectionLeft || _arrowDirection == YBPopupMenuArrowDirectionRight) {
- }
- }
- - (CGFloat)getMenuTotalHeight
- {
- CGFloat menuHeight = 0;
- if (_titles.count > _maxVisibleCount) {
- menuHeight = _itemHeight * _maxVisibleCount + _borderWidth * 2;
- }else {
- menuHeight = _itemHeight * _titles.count + _borderWidth * 2;
- }
- return menuHeight;
- }
- - (void)drawRect:(CGRect)rect
- {
- UIBezierPath *bezierPath = [YBPopupMenuPath yb_bezierPathWithRect:rect rectCorner:_rectCorner cornerRadius:_cornerRadius borderWidth:_borderWidth borderColor:_borderColor backgroundColor:_backColor arrowWidth:_arrowWidth arrowHeight:_arrowHeight arrowPosition:_arrowPosition arrowDirection:_arrowDirection];
- [bezierPath fill];
- [bezierPath stroke];
- }
- @end
|