(1)自定義KKActSheetView繼承于UIView。
.h文件內(nèi)容
#importtypedef void(^ActsheetBlock)(NSInteger tag);@interface KKActSheetView : UIView{
? ? UITableView *table;
? ? UILabel *titlebel;
}
@property (nonatomic,strong)ActsheetBlock ActBlock;
@property (nonatomic,strong)NSMutableArray *datasourceData;
.m文件內(nèi)容
#import "KKActSheetView.h"
#import "UIColor+Factory.h"
#import "UIView+RMAdditions.h"
@implementation KKActSheetView
-(NSMutableArray *)datasourceData{
? ? if (_datasourceData == nil) {
? ? ? ? _datasourceData =[NSMutableArray arrayWithObjects:
? ? ? ? ? ? ? ? ? ? ? ? ? @"拍攝照片",
? ? ? ? ? ? ? ? ? ? ? ? ? @"拍攝視頻",
? ? ? ? ? ? ? ? ? ? ? ? ? @"相冊選視頻",
? ? ? ? ? ? ? ? ? ? ? ? ? @"相冊選照片",
? ? ? ? ? ? ? ? ? ? ? ? ? @"取消",
? ? ? ? ? ? ? ? ? ? ? ? ? nil];
? ? }
? ? return _datasourceData;
}
-(instancetype)init{
? ? self=[super initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
? ? if (self) {
? ? ? ? //蒙版
? ? ? ? self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
? ? ? ? table = [[UITableView alloc] initWithFrame:CGRectMake(0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.height,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.width,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 247)];
? ? ? ? [self addSubview:table];
? ? ? ? table.delegate? = self;
? ? ? ? table.dataSource = self;
? ? ? ? [table setBackgroundColor:UIColorFromRGB(0xC0C0C0)];
? ? ? ? table.scrollEnabled = NO;
? ? ? ? [table setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
? ? ? ? [UIView animateWithDuration:0.25 animations:^{
//初始化table,先讓其置于屏幕下方,便于在點(diǎn)擊的時(shí)候從屏幕底部彈出
? ? ? ? ? ? table.frame = CGRectMake(0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.height - 247,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.width,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 247);
? ? ? ? }];
? ? ? ? [self datasourceData];
? ? }
? ? return self;
}
#pragma mark ---delegate-datasources--
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
? ? return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
? ? return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * iden = @"iden";
? ? UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:iden];
? ? if (cell == nil) {
? ? ? ? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
? ? ? ? titlebel =[[UILabel alloc] initWithFrame:CGRectMake(0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (cell.frame.size.height-20)/2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.width,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 20)];
? ? ? ? [cell.contentView addSubview:titlebel];
? ? ? ? [titlebel setTextAlignment:NSTextAlignmentCenter];
? ? ? ? [titlebel setText:[_datasourceData objectAtIndex:indexPath.row]];
? ? ? ? [titlebel setFont:[UIFont fontWithName:@"PingFangSC-Light" size:16]];
? ? }
? ? if (indexPath.row == 4) {
? ? ? ? titlebel.hidden = YES;
? ? ? ? UILabel * line =[[UILabel alloc] initWithFrame:CGRectMake(0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.width,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 10)];
? ? ? ? [cell.contentView addSubview:line];
? ? ? ? line.backgroundColor=[UIColor grayColor];
? ? ? ? UILabel * cancellabe =[[UILabel alloc] initWithFrame:CGRectMake(0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? line.bottom + 10,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.frame.size.width,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 20)];
? ? ? ? [cell.contentView addSubview:cancellabe];
? ? ? ? [cancellabe setTextAlignment:NSTextAlignmentCenter];
? ? ? ? [cancellabe setText:[_datasourceData objectAtIndex:indexPath.row]];
? ? ? ? [cancellabe setFont:[UIFont fontWithName:@"PingFangSC-Light" size:16]];
? ? }
? ? return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? return (247 / 5);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
? ? if (_ActBlock) {
? ? ? ? _ActBlock(indexPath.row);
? ? }
? ? [self remove];
}
-(void)remove{
[UIView animateWithDuration:0.25 animations:^{
? ? table.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 247);
? ? [self removeFromSuperview];
}];
}
#pragma mark - 補(bǔ)全分隔線左側(cè)缺失
- (void)viewDidLayoutSubviews {
? ? if ([table respondsToSelector:@selector(setSeparatorInset:)]) {
? ? ? ? [table setSeparatorInset:UIEdgeInsetsZero];
? ? }
? ? if ([table respondsToSelector:@selector(setLayoutMargins:)])? {
? ? ? ? [table setLayoutMargins:UIEdgeInsetsZero];
? ? }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
? ? if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
? ? ? ? [cell setLayoutMargins:UIEdgeInsetsZero];
? ? }
? ? if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
? ? ? ? [cell setSeparatorInset:UIEdgeInsetsZero];
? ? }
}
謝謝支持!!!