效果图
Untitled.gif
//
// CBPopMenuView.h
// popup
//
// Created by 王木木 on 2018/6/13.
// Copyright © 2018年 wangmumu. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^CallBlcokIndex) (NSInteger index);
@interface CBPopMenuView : UIView
@property (nonatomic,copy) CallBlcokIndex callBlcokIndex;
@property (nonatomic, strong) UITableView * tableView;
+ (void)showRelyOnView:(UIView *)view titles:(NSArray *)titles menuWidth:(CGFloat)itemWidth block:(CallBlcokIndex)block;
@end
//
// CBPopMenuView.m
// popup
//
// Created by 王木木 on 2018/6/13.
// Copyright © 2018年 wangmumu. All rights reserved.
//
#define YBScreenWidth [UIScreen mainScreen].bounds.size.width
#define YBScreenHeight [UIScreen mainScreen].bounds.size.height
#define YBMainWindow [UIApplication sharedApplication].keyWindow
#import "CBPopMenuView.h"
@interface CBPopupMenuCell : UITableViewCell
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView * lineView;
@end
@implementation CBPopupMenuCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createUI];
}
return self;
}
- (void)createUI {
self.nameLabel = [[UILabel alloc] init];
self.nameLabel.font = [UIFont systemFontOfSize:14.0];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.nameLabel];
self.lineView = [[UIView alloc] init];
self.lineView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
[self addSubview:self.lineView];
}
@end
@interface CBPopMenuView ()<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, strong) UIView * menuBackView;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, strong) NSArray * titles;
@property (nonatomic) CGRect relyRect;
@property (nonatomic) CGPoint point;
@property (nonatomic) CGFloat topDistance;
@end
@implementation CBPopMenuView
+ (void)showRelyOnView:(UIView *)view titles:(NSArray *)titles menuWidth:(CGFloat)itemWidth block:(CallBlcokIndex)block {
CGRect absoluteRect = [view convertRect:view.bounds toView:YBMainWindow];
CGPoint relyPoint = CGPointMake(absoluteRect.origin.x + absoluteRect.size.width / 2, absoluteRect.origin.y + absoluteRect.size.height);
CBPopMenuView *popupMenu = [[CBPopMenuView alloc] init];
popupMenu.callBlcokIndex = block;
popupMenu.itemWidth = itemWidth;
popupMenu.titles = titles;
popupMenu.point = relyPoint;
popupMenu.relyRect = absoluteRect;
popupMenu.itemHeight = 44;
popupMenu.tableView.frame = CGRectMake(relyPoint.x - itemWidth/2 , relyPoint.y, itemWidth, titles.count * popupMenu.itemHeight);
[popupMenu.tableView reloadData];
[popupMenu show];
}
- (instancetype)init
{
self = [super init];
if (self) {
[self setDefaultSettings];
}
return self;
}
- (void)setDefaultSettings {
self.frame = CGRectMake(0, 0, YBScreenWidth, YBScreenHeight);
_menuBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YBScreenWidth, YBScreenHeight)];
_menuBackView.backgroundColor = [UIColor clearColor];
_menuBackView.alpha = 0;
_topDistance = 10;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(touchOutSide)];
[_menuBackView addGestureRecognizer: tap];
[self addSubview:_menuBackView];
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.tableView];
}
- (void)touchOutSide {
[self dismiss];
}
- (void)show {
[YBMainWindow addSubview:self];
self.tableView.frame = CGRectMake(self.point.x - self.itemWidth/2, self.point.y + self.topDistance, self.itemWidth, 0);
[UIView animateWithDuration: 0.25 animations:^{
self.tableView.frame = CGRectMake(self.point.x - self.itemWidth/2, self.point.y + self.topDistance, self.itemWidth, self.titles.count * self.itemHeight);
} completion:^(BOOL finished) {
}];
}
- (void)dismiss
{
[UIView animateWithDuration: 0.25 animations:^{
self.tableView.frame = CGRectMake(self.point.x - self.itemWidth/2, self.point.y + self.topDistance, self.itemWidth, 0);
} completion:^(BOOL finished) {
[self removeFromSuperview];
[self.menuBackView removeFromSuperview];
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self dismiss];
}
#pragma mark tableViewDelegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CBPopupMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[CBPopupMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.lineView.frame = CGRectMake(15, self.itemHeight - 1,self.itemWidth - 30, 0.5);
cell.nameLabel.frame = CGRectMake(0, 0,self.itemWidth, self.itemHeight);
cell.nameLabel.text = self.titles[indexPath.row];
cell.lineView.hidden = indexPath.row == (self.titles.count - 1);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _itemHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self dismiss];
self.callBlcokIndex ? self.callBlcokIndex(indexPath.row) : nil;
}
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor yellowColor];
_tableView.tableFooterView = [UIView new];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return _tableView;
}
@end
使用
NSArray *titles = @[@"全部", @"收入", @"支出"];
[CBPopMenuView showRelyOnView:btn titles:titles menuWidth:150 block:^(NSInteger index) {
NSLog(@"%ld",(long)index);
}];
网友评论