iOS每日一记之———————————————一个弹框View

此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




最后效果图 如下

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值