首页 > 其他分享 >iOS开发之自定义ActionSheet视图

iOS开发之自定义ActionSheet视图

时间:2022-11-28 17:06:33浏览次数:67  
标签:indexPath return 自定义 tableView self ActionSheet 视图 cell UITableView


有时我们需要用到actionSheet来展示,但是但是往往系统的界面显示很丑或者并不符合UI的要求,所以在这里自定义一个,方便以后使用,后续有时间写一下Swift的开发。

自定义ActionSheet的关键点,就是UI的样式修改和设计调整,还有就是点击单元格时进行的后续操作,再一个就是界面显示的平滑度。

首先界面设计:

创建一个半透明的背景视图;

然后一个表格,表格分成两个区,设置标题头、区尾和单元格边角


//背景
- (UIView*)maskView {
if (!_maskView) {
_maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_maskView.backgroundColor = [UIColor blackColor];
_maskView.alpha = 0.5;
_maskView.userInteractionEnabled = YES;
}
return _maskView;
}
//表格
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.layer.cornerRadius = 10;
_tableView.clipsToBounds = YES;
_tableView.rowHeight = 44.0;
_tableView.bounces = NO;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.tableHeaderView = self.headView;
_tableView.separatorInset = UIEdgeInsetsMake(0, -50, 0, 0);
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"OneCell"];
}
return _tableView;
}
#pragma mark <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (section == 0)?_cellArray.count:1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneCell"];
if (indexPath.section == 0) {
cell.textLabel.text = _cellArray[indexPath.row];
if (indexPath.row == _cellArray.count - 1) {

//添加贝塞尔曲线,UIBezierPath与CAShapeLayer设计边角样式
/*
byRoundingCorners即为设置所需处理边角参数,有如下枚举克进行选择:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,//左上圆角
UIRectCornerTopRight = 1 << 1,//右上圆角
UIRectCornerBottomLeft = 1 << 2,//左下圆角
UIRectCornerBottomRight = 1 << 3,//右下圆角
UIRectCornerAllCorners = ~0UL //四角圆角
};
*/
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, Screen_Width - (Space_Line * 2), tableView.rowHeight) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = cell.contentView.bounds;
maskLayer.path = maskPath.CGPath;
cell.layer.mask = maskLayer;
}
} else {
cell.textLabel.text = _cancelTitle;
cell.layer.cornerRadius = 10;
}
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (self.selectedBlock) {
self.selectedBlock(indexPath.row);
}
} else {
if (self.cancelBlock) {
self.cancelBlock();
}
}
[self dismiss];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return Space_Line;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, Space_Line)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}

界面设计完成,需要考虑的就是弹出、消失的问题


/滑动弹出
- (void)show {
_tableView.frame = CGRectMake(Space_Line, Screen_Height, Screen_Width - (Space_Line * 2), _tableView.rowHeight * (_cellArray.count + 1) + _headView.bounds.size.height + (Space_Line * 2));
[UIView animateWithDuration:.5 animations:^{
CGRect rect = _tableView.frame;
rect.origin.y -= _tableView.bounds.size.height;
_tableView.frame = rect;
}];
}
//滑动消失
- (void)dismiss {
[UIView animateWithDuration:.5 animations:^{
CGRect rect = _tableView.frame;
rect.origin.y += _tableView.bounds.size.height;
_tableView.frame = rect;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
#pragma mark ------ 触摸屏幕其他位置弹下
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self dismiss];
}

最后是对自定义的视图的调用:


//弹出ActionSheet
__weak typeof(self) weakSelf = self;
JasonActionSheetView *jasonSheetView = [[JasonActionSheetView alloc]initWithHeadView:self.headView cellArray:self.dataArr cancelTitle:@"取消" selectedBlock:^(NSInteger index) {
//点击单元格后续操作
if (index == 0) {
weakSelf.view.backgroundColor = [UIColor redColor];
}else if(index == 1){
weakSelf.view.backgroundColor = [UIColor yellowColor];
}else{
weakSelf.view.backgroundColor = [UIColor lightGrayColor];
}
} cancelBlock:^{
NSLog(@"点击了取消........");
}];

[self.view addSubview:jasonSheetView];

效果图:

iOS开发之自定义ActionSheet视图_弹出框



​源码:https://github.com/hbblzjy/OC-JasonActionSheet​




标签:indexPath,return,自定义,tableView,self,ActionSheet,视图,cell,UITableView
From: https://blog.51cto.com/u_15894905/5892245

相关文章

  • iOS开发之样式多样好用的滑动视图Demo
    现在App基本上都有滑动式图的展示,尤其是新闻资讯类的用到的最多,今天就给大家展示一个多样的滑动视图样式;首先可以看到,这是几种不同的搭配模式,也可以根据style进行其他样式的......
  • Swift基础之封装蒙版指导视图
    相信大家都见到过,一个软件添加了新功能,会给用户使用步骤指导,所以我针对这个功能,便于使用的小demo,希望对大家有帮助。源码中的注释比较详细,这里不再赘述,自行研究:varimageNam......
  • 自定义UICollectionViewController之后如何设置布局方式
    今天使用了自定义UICollectionViewController,发现了布局问题,所以给初学者讲解一下,当我们自定义了UICollectionViewController就无法设置UICollectionView的布局样式的问题......
  • 自定义中文全文索引
    自定义中文全文索引​​一、中文分词插件​​​​1、分词组件的调整​​​​2、分词测试​​​​二、样例数据准备​​​​三、通过中文全文分词组件创建节点索引​​​​四......
  • 自定义sublime text 2 build system
    IntroductionSublimeText ​​buildsystems​​ canbeconsideredsimplistic,buthighlycustomizable.ThebasicideaisthateachtypeofBuildprofileispow......
  • 企业级自定义表单引擎解决方案(十七)--Job配置执行
    .netcore研发的低代码自定义表单引擎,采用强大的规则引擎将所有的业务串联起来的,和其他低代码平台是有本质的区别的,目标是完全解放繁琐的CRUD工作。常规的业务,在需求以及......
  • 【自定义控件】WrapBeakPanel-自定义代有换行功能的Panel
    自定义代有换行功能的PanelWrapBreakPanel.csusingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Windows.Controls;usingSystem.Win......
  • MySQL进阶实战6,缓存表、视图、计数器表
    一、缓存表和汇总表有时提升性能最好的方法是在同一张表中保存衍生的冗余数据,有时候还需要创建一张完全独立的汇总表或缓存表。缓存表用来存储那些获取很简单,但速度较慢的数......
  • 判断素数(自定义函数)
    这篇和前面那个不同,是用自定义函数来写的#pragmawarning(disable:4996)#include<stdio.h>#include<math.h>//sqrt的头文件//sqrt(X)-对括号里的数开平方intpanduansushu(int......
  • DRF过滤、排序、异常处理、自定义Response、分页
    DRF过滤、排序、异常处理、自定义Response、分页目录DRF过滤、排序、异常处理、自定义Response、分页过滤局部过滤排序异常处理封装Response对象分页三种分页方式PageNumb......