IOS轻松搞定自定义ActionSheet(类似微信选择图库,相机)

(1)自定义KKActSheetView继承于UIView。

.h文件内容

#importtypedef void(^ActsheetBlock)(NSInteger tag);@interface KKActSheetView : UIView{

    UITableView *table;

    UILabel *titlebel;

}

@property (nonatomic,strong)ActsheetBlock ActBlock;

@property (nonatomic,strong)NSMutableArray *datasourceData;

.m文件内容

#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,先让其置于屏幕下方,便于在点击的时候从屏幕底部弹出

            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 - 补全分隔线左侧缺失

- (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];

    }

}

谢谢支持!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,088评论 3 38
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 946评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,542评论 1 14
  • 用一下午的时间,一口气看完了大话西游系列,《大话西游》、《大话西游一一大圣娶亲》,《大话西游一一月光宝盒》三部。 ...
    九思猫阅读 194评论 0 1
  • 乌云蔽日,星月无光! 烈风携着冷雨飘扬! 尘!微扬!花!渐落! 冷雨轻抚大...
    淡忘年轮阅读 227评论 0 1