Skip to content

Commit 9a5c2f9

Browse files
committed
add arrow on top of content view
1 parent db0ea66 commit 9a5c2f9

4 files changed

Lines changed: 52 additions & 6 deletions

File tree

FEPopupMenuController/FEPopupMenuController.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@
6565
*/
6666
@property (nonatomic, strong) UIColor *itemTitleColor;
6767

68+
/**
69+
* arrow view x position , default is 70% of contentViewWidth
70+
*/
71+
@property (nonatomic, assign) CGFloat arrowX;
72+
73+
/**
74+
* arrow of top on content view show or hidden, default is NO (hidden)
75+
*/
76+
@property (nonatomic, assign) BOOL isShowArrow;
77+
6878
@end

FEPopupMenuController/FEPopupMenuController.m

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
#import "FEPopupMenuControllerAnimatedTransitioning.h"
1212

1313
static const CGFloat kDefaultContentViewWidth = 130.0;
14+
static const CGFloat kDefaultArrowWeight = 10.0;
15+
static const CGFloat kDefaultArrowHeight = 7.0;
1416

1517
@interface FEPopupMenuController () <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
1618

1719
@property (nonatomic, strong) UIView *contentView;
1820
@property (nonatomic, strong) UITableView *tableView;
1921
@property (nonatomic, strong) UITapGestureRecognizer *tapBackgroundGestureRecognizer;
22+
@property (nonatomic, strong) UIView *arrowView;
2023

2124
@end
2225

@@ -30,6 +33,8 @@ -(instancetype)initWithItems:(NSArray<FEPopupMenuItem *> *)items {
3033
self.contentViewPosition = CGPointMake(0,0);
3134
self.contentViewBackgroundColor = [UIColor whiteColor];
3235
self.contentViewCornerRadius = 8.0;
36+
self.arrowX = self.contentViewWidth * 0.7;
37+
self.isShowArrow = NO;
3338
}
3439
return self;
3540
}
@@ -50,18 +55,27 @@ - (void)viewDidLoad {
5055
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(self.contentViewPosition.x,
5156
self.contentViewPosition.y,
5257
self.contentViewWidth,
53-
44 * [self.items count])];
54-
self.contentView.layer.cornerRadius = self.contentViewCornerRadius;
55-
self.contentView.clipsToBounds = YES;
58+
44 * [self.items count] + kDefaultArrowHeight)];
59+
60+
61+
// arrow
62+
if (self.isShowArrow) {
63+
self.arrowView.frame = CGRectMake(self.arrowX, 0, self.arrowView.bounds.size.width, self.arrowView.bounds.size.height);
64+
[self.contentView addSubview:self.arrowView];
65+
}
5666

5767
// init TableView
58-
self.tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds];
68+
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kDefaultArrowHeight,
69+
self.contentView.bounds.size.width,
70+
self.contentView.bounds.size.height - kDefaultArrowHeight)];
5971
[self.tableView registerNib:[FEPopupMenuItemCell nib] forCellReuseIdentifier:[FEPopupMenuItemCell identifier]];
6072
self.tableView.delegate = self;
6173
self.tableView.dataSource = self;
6274
self.tableView.separatorColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
6375
self.tableView.scrollEnabled = NO;
6476
self.tableView.backgroundColor = self.contentViewBackgroundColor;
77+
self.tableView.layer.cornerRadius = self.contentViewCornerRadius;
78+
self.tableView.clipsToBounds = YES;
6579

6680
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
6781
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
@@ -185,4 +199,26 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive
185199
return transitioning;
186200
}
187201

202+
#pragma mark -
203+
204+
- (UIView *)arrowView{
205+
if (!_arrowView) {
206+
// draw
207+
CGSize size = CGSizeMake(kDefaultArrowWeight, kDefaultArrowHeight);
208+
UIBezierPath *path = [[UIBezierPath alloc] init];
209+
[path moveToPoint:CGPointMake(size.width / 2.0, 0)];
210+
[path addLineToPoint:CGPointMake(0, size.height)];
211+
[path addLineToPoint:CGPointMake(size.width, size.height)];
212+
path.lineWidth = 1.0;
213+
214+
CAShapeLayer *arrowLayer = [CAShapeLayer layer];
215+
arrowLayer.path = path.CGPath;
216+
217+
_arrowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
218+
_arrowView.layer.mask = arrowLayer;
219+
_arrowView.backgroundColor = self.contentViewBackgroundColor;
220+
}
221+
return _arrowView;
222+
}
223+
188224
@end

FEPopupMenuController/FEPopupMenuItem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef void(^FEPopupMenuItemActionBlock)(void);
1616
/**
1717
* init FEPopupMenuItem with title text and icon image
1818
*/
19-
- (instancetype)initWithTitle:(NSString *)title iconImage:(UIImage *)iconImage action:(FEPopupMenuItemActionBlock)action;
19+
- (instancetype __nonnull)initWithTitle:(NSString *)title iconImage:(UIImage *)iconImage action:(FEPopupMenuItemActionBlock)action;
2020

2121
/**
2222
* title of item

FEPopupMenuControllerDemo/FEPopupMenuControllerDemo/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ - (IBAction)popMenuAction:(UIBarButtonItem *)sender {
5353
*/
5454
- (CGPoint)calculateMenuShowPoint{
5555
// get a position on screen (top right corner)
56-
return CGPointMake(CGRectGetWidth(self.view.frame) - self.popupMenuController.contentViewWidth - 20, 64 + 10);;
56+
return CGPointMake(CGRectGetWidth(self.view.frame) - self.popupMenuController.contentViewWidth - 20, 64);;
5757
}
5858

5959
@end

0 commit comments

Comments
 (0)