此View的效果和QQ 微信 点击加号的效果相同
主要由着五个文件组成
首先MoreSettingMenu.h
#import <UIKit/UIKit.h>
@protocol MoreSettingMenuDelegate <UITableViewDataSource,UITableViewDelegate>
@end
@interface MoreSettingMenu : UIView
- (void) setData : (NSArray *) data;
- (void) setImageData :(NSArray *)imageArrs;
- (void) showInView : (UIView *) view;
- (void) dismiss;
@property (nonatomic, copy) void(^menuClickAtIndex)(NSInteger item);
@property (nonatomic, copy) void(^menuDismissed)();
@end
接着MoreSetting.m文件
#import "MoreSettingMenu.h"
#import "Constants.h"
#import "MoreSettingMenuCell.h"
@interface MoreSettingMenu()<UITableViewDataSource,UITableViewDelegate> {
NSMutableArray *_data;
NSMutableArray *_imageArrs;
}
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation MoreSettingMenu
//table的宽度
static const NSInteger TABLE_WIDTH = 140;
static const NSInteger TABLE_MARGIN_RIGHT = 8;
//cell的高度
static const NSInteger CELL_HEIGHT = 34;
- (id) init {
self = [super init];
if(self) {
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//蒙版
_bgView = [[UIView alloc] initWithFrame:self.bounds];
_bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
[self addSubview:_bgView];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
recognizer.numberOfTouchesRequired = 1;
[_bgView addGestureRecognizer:recognizer];
UIImageView *imageView = [[UIImageView alloc]init];
self.imageView =imageView;
imageView.image = [UIImage imageNamed:@"tankuang_bg"];
imageView.frame = CGRectMake(SCREEN_WIDTH - TABLE_WIDTH - TABLE_MARGIN_RIGHT, 0, TABLE_WIDTH, 0);
[self addSubview:imageView];
_table = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TABLE_WIDTH - TABLE_MARGIN_RIGHT, 10, TABLE_WIDTH, 0) style:UITableViewStylePlain];
[_table registerNib:[UINib nibWithNibName:@"MoreSettingMenuCell" bundle:nil] forCellReuseIdentifier:@"MoreSettingMenuIdentifier"];
_table.separatorStyle = UITableViewCellSeparatorStyleNone;
_table.delegate = self;
_table.dataSource = self;
_data = [NSMutableArray array];
_imageArrs = [NSMutableArray array];
[self addSubview:_table];
// self.hidden = YES;
}
return self;
}
- (void) setData:(NSArray *)data {
if(_data.count > 0) {
[_data removeAllObjects];
}
[_data addObjectsFromArray:data];
[_table reloadData];
}
- (void) setImageData :(NSArray *)imageArrs{
if(_imageArrs.count > 0) {
[_imageArrs removeAllObjects];
}
[_imageArrs addObjectsFromArray:imageArrs];
[_table reloadData];
}
- (void) removeFromSuperview {
[super removeFromSuperview];
}
- (void) dismiss : (UITapGestureRecognizer *) recognizer {
[self dismiss];
}
- (void) showInView:(UIView *)view {
if(self.isHidden) {
self.hidden = NO;
}
NSInteger tableHeight = CELL_HEIGHT * _data.count;
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = _table.frame;
frame.size.height = tableHeight;
_table.frame = frame;
}];
NSInteger imageViewHeight = CELL_HEIGHT * _data.count;
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.imageView.frame;
frame.size.height = imageViewHeight;
self.imageView.frame = frame;
}];
}
- (void) dismiss {
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = _table.frame;
frame.size.height = 0;
_table.frame = frame;
} completion:^(BOOL finished) {
if (self.menuDismissed) {
self.menuDismissed();
}
self.hidden = YES;
}];
}
#pragma mark - uitableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return CELL_HEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.menuClickAtIndex(indexPath.row);
[self dismiss];
}
#pragma mark - uitableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MoreSettingMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreSettingMenuIdentifier"];
cell.label.text = _data[indexPath.row];
cell.logImageView.image = [UIImage imageNamed:_imageArrs[indexPath.row]];
return cell;
}
@end
然后是MoreSeetingCell文件 里面其实就创建一个label 和 一个imageView
最后效果图 如下