IOS輕松搞定自定義ActionSheet(類似微信選擇圖庫(kù),相機(jī))

(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:

? ? ? ? ? ? ? ? ? ? ? ? ? @"拍攝照片",

? ? ? ? ? ? ? ? ? ? ? ? ? @"拍攝視頻",

? ? ? ? ? ? ? ? ? ? ? ? ? @"相冊(cè)選視頻",

? ? ? ? ? ? ? ? ? ? ? ? ? @"相冊(cè)選照片",

? ? ? ? ? ? ? ? ? ? ? ? ? @"取消",

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

? ? }

}

謝謝支持?。?!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,289評(píng)論 3 38
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個(gè)文章 AppDelegate.h ...
    CC_iOS閱讀 1,182評(píng)論 0 0
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,726評(píng)論 1 14
  • 用一下午的時(shí)間,一口氣看完了大話西游系列,《大話西游》、《大話西游一一大圣娶親》,《大話西游一一月光寶盒》三部。 ...
    九思貓閱讀 226評(píng)論 0 1
  • 烏云蔽日,星月無(wú)光! 烈風(fēng)攜著冷雨飄揚(yáng)! 塵!微揚(yáng)!花!漸落! 冷雨輕撫大...
    淡忘年輪閱讀 246評(píng)論 0 1

友情鏈接更多精彩內(nèi)容