有时候我们在开发中,系统提供的actionsheet 不能满足我们的需求,所以,今天就做一个类似微信中的,支持多个按钮,我见有的人用的是个tableview,也可以,但是有点麻烦.

效果图:

ActionSheetItem:

#import <Foundation/Foundation.h>

@interface ActionSheetItem : NSObject
//点击title
@property (nonatomic,copy) NSString *title;
//点击的index
@property (nonatomic,assign) NSInteger index; + (ActionSheetItem *)itemWithTitle:(NSString *)title index:(NSInteger)index; @end #import "ActionSheetItem.h" @implementation ActionSheetItem + (ActionSheetItem *)itemWithTitle:(NSString *)title index:(NSInteger)index { ActionSheetItem *sheetItem = [[ActionSheetItem alloc] initWithTitle:title index:index];
return sheetItem;
} - (instancetype)initWithTitle:(NSString *)title index:(NSInteger)index {
self = [super init];
if(self) {
_title = title;
_index = index;
}
return self;
} @end

ActionSheetView:

#import <UIKit/UIKit.h>
#import "ActionSheetItem.h" typedef void(^ClickBlock)(ActionSheetItem *sheetItem); @interface ActionSheetView : UIView @property (nonatomic,copy)ClickBlock clickBlock; - (instancetype)initWithCancleTitle:(NSString *)cancleTitle
otherTitles:(NSString *)otherTitles,... NS_REQUIRES_NIL_TERMINATION; - (void)show; @end
//
// ActionSheetView.m
// ActionSheet
//
// Created by Doman on 17/4/17.
// Copyright © 2017年 doman. All rights reserved.
// #import "ActionSheetView.h" static CGFloat BtnHeight = 40.0;//每个按钮的高度
static CGFloat CancleMargin = 6.0;//取消按钮上面的间隔
#define ActionSheetColor(r, g, b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]
#define ActionSheetBGColor ActionSheetColor(237,240,242) //背景色
#define ActionSheetSeparatorColor ActionSheetColor(226, 226, 226) //分割线颜色
#define ActionSheetNormalImage [self imageWithColor:ActionSheetColor(255,255,255)] //普通下的图片
#define ActionSheetHighImage [self imageWithColor:ActionSheetColor(242,242,242)] //高粱的图片
#define ActionSheetBlueNormalImage [self imageWithColor:ActionSheetColor(100,175,247)] //普通下的图片 #define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height @interface ActionSheetView () @property (nonatomic, strong) UIView *sheetView;
@property (nonatomic, copy) NSMutableArray *items; @end @implementation ActionSheetView - (instancetype)initWithCancleTitle:(NSString *)cancleTitle otherTitles:(NSString *)otherTitles, ...
{
self = [super init];
if (self) {
//黑色遮盖
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication].keyWindow addSubview:self];
self.alpha = 0.0;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
[self addGestureRecognizer:tap]; // sheet
_sheetView = [[UIView alloc] initWithFrame:CGRectMake(, , ScreenWidth, )];
_sheetView.backgroundColor = ActionSheetColor(,,);
_sheetView.alpha = 0.9;
[[UIApplication sharedApplication].keyWindow addSubview:_sheetView];
_sheetView.hidden = YES; int tag = ;
_items = [NSMutableArray array];
//首先添加取消按钮
ActionSheetItem *cancleItem = [ActionSheetItem itemWithTitle:@"取消" index:];
[_items addObject:cancleItem]; tag ++; NSString* curStr;
va_list list;
if(otherTitles)
{
ActionSheetItem *item = [ActionSheetItem itemWithTitle:otherTitles index:tag];
[_items addObject:item];
tag ++; va_start(list, otherTitles);
while ((curStr = va_arg(list, NSString*))) {
ActionSheetItem *item = [ActionSheetItem itemWithTitle:curStr index:tag];
[_items addObject:item];
tag ++;
}
va_end(list);
}
CGRect sheetViewF = _sheetView.frame;
sheetViewF.size.height = BtnHeight * _items.count + CancleMargin;
_sheetView.frame = sheetViewF;
//开始添加按钮
[self setupBtnWithTitles];
}
return self; } // 创建每个选项
- (void)setupBtnWithTitles { for (ActionSheetItem *item in _items) {
UIButton *btn = nil;
if (item.index == ) {//取消按钮
btn = [[UIButton alloc] initWithFrame:CGRectMake(, _sheetView.frame.size.height - BtnHeight, ScreenWidth, BtnHeight)]; } else {
btn = [[UIButton alloc] initWithFrame:CGRectMake(, BtnHeight * (item.index - ) , ScreenWidth, BtnHeight)];
// 最上面画分割线
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(, , ScreenWidth, 0.5)];
line.backgroundColor = ActionSheetSeparatorColor; [btn addSubview:line]; }
btn.tag = item.index; [btn setBackgroundImage:ActionSheetNormalImage forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundImage:ActionSheetHighImage forState:UIControlStateHighlighted];
[btn setTitle:item.title forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:];
[btn addTarget:self action:@selector(sheetBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.sheetView addSubview:btn];
}
}
- (void)show {
self.sheetView.hidden = NO; CGRect sheetViewF = self.sheetView.frame;
sheetViewF.origin.y = ScreenHeight;
self.sheetView.frame = sheetViewF; CGRect newSheetViewF = self.sheetView.frame;
newSheetViewF.origin.y =ScreenHeight - self.sheetView.frame.size.height; [UIView animateWithDuration:0.3 animations:^{ self.sheetView.frame = newSheetViewF; self.alpha = 0.3;
}];
} // 显示黑色遮罩
- (void)coverClick{
CGRect sheetViewF = self.sheetView.frame;
sheetViewF.origin.y = ScreenHeight; [UIView animateWithDuration:0.2 animations:^{
self.sheetView.frame = sheetViewF;
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
[self.sheetView removeFromSuperview];
}];
} - (void)sheetBtnClick:(UIButton *)btn{
ActionSheetItem *item = _items[btn.tag]; if (item.index == ) {
[self coverClick];
return;
}
if (self.clickBlock) {
self.clickBlock(item);
} [self coverClick];
} //根据颜色生成图片
- (UIImage*)imageWithColor:(UIColor*)color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
} @end

调用:

    ActionSheetView *sheetView = [[ActionSheetView alloc] initWithCancleTitle:@"取消" otherTitles:@"",@"" ,@"",@"",nil];

    [sheetView show];
sheetView.clickBlock = ^(ActionSheetItem *item)
{
NSLog(@"%@--%zd",item.title,item.index);
};

Demo地址: https://github.com/domanc/ActionSheet.git

仿微信-ActionSheet的更多相关文章

  1. iOS开发-仿微信图片分享界面实现

    分享功能目前几乎已成为很多app的标配了,其中微信,微博等app的图片分享界面设计的很棒,不仅能够展示缩略图,还可以预览删除.最近我在做一款社交分享app,其中就要实现图文分享功能,于是试着自行实现仿 ...

  2. Nuxt+Vue聊天室|nuxt仿微信App界面|nuxt.js聊天实例

    一.项目简述 nuxt-chatroom 基于Nuxt.js+Vue.js+Vuex+Vant+VPopup等技术构建开发的仿微信|探探App界面社交聊天室项目.实现了卡片式翻牌滑动.消息发送/emo ...

  3. Android -- 真正的 高仿微信 打开网页的进度条效果

    (本博客为原创,http://www.cnblogs.com/linguanh/) 目录: 一,为什么说是真正的高仿? 二,为什么要搞缓慢效果? 三,我的实现思路 四,代码,内含注释 五,使用方法与截 ...

  4. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  5. 小程序用户反馈 - HotApp小程序统计仿微信聊天用户反馈组件,开源

    用户反馈是小程序开发必要的一个功能,但是和自己核心业务没关系,主要是产品运营方便收集用户的对产品的反馈.HotApp推出了用户反馈的组件,方便大家直接集成使用 源码下载地址: https://gith ...

  6. GSD_WeiXin(高仿微信)应用源码

    高仿微信计划:已经实现功能 1.微信首页(cell侧滑编辑.下拉眼睛动画.下拉拍短视频.点击进入聊天详情界面) 2.通讯录(联系人字母排序.搜索界面) 3.发现(朋友圈) 4.我(界面) 待实现功能( ...

  7. Android仿微信拍摄短视频

    近期做项目需要添加上传短视频功能,功能设置为类似于微信,点击开始拍摄,设置最长拍摄时间,经过研究最终实现了这个功能,下面就和大家分享一下,希望对你有帮助. 1.视频录制自定义控件: /** * 视频播 ...

  8. CSS3 仿微信聊天小气泡

    今天给大家分享一个我刚做的项目中的一个小案例, 因为我们在做一个聊天的功能,之前的聊天页面UI很丑,我就不在这里展示给大家了. 现在就教大家怎么用css3制作一个和微信聊天界面一样的页面. 首先给大家 ...

  9. jquerymobile仿微信 - 01

    jquerymobile仿微信 - 01 jquerymobile的组件感觉不咋地哇 本地调试最好是开一个web server,不然数据访问会有问题 <div data-role="p ...

随机推荐

  1. laravel的学习历程

    首要,表明态度:PHP是世界上最佳的言语.(梗) laravel说是php将来,形似不假. 最开端触摸的是thinkphp,格外喜爱她的分层,文档格外完全,阅读起来没任何妨碍. 比较laravel,我 ...

  2. nginx+php测试时显示 502 bad gateway的解决方法

    http://www.apelearn.com/study_v2/chapter18.html 由于阿铭老师的PHP版本是 5.3的   我装了 5.5 测试出现了 502  错误 查看日志   借助 ...

  3. nagios(centreon)监控Linux日志

    1 将check_log3.pl下载后放到客户端服务器的插件文件夹[root@SSAVL2475 libexec]# cp /tmp/check_log3.pl  /usr/local/nagios/ ...

  4. php的闭包

    闭包是指在创建时封装周围状态的函数,即使闭包所在的环境的不存在了,闭包中封装的状态依然存在. 匿名函数其实就是没有名称的函数,匿名函数可以赋值给变量,还能像其他任何PHP函数对象那样传递.不过匿名函数 ...

  5. python接口自动化19-requests-toolbelt处理multipart/form-data

    requests-toolbelt 1.官方文档地址:requests-toolbelt官方文档 2.环境安装 pip install requests-toolbelt multipart/form ...

  6. Java 方法签名

    方法签名格式: 方法名   参数列表 例如: public class A{ protected int method (int a, int b) { return 0; } } class B e ...

  7. Tkinter Message

    Python GUI - Tkinter Message(消息):这个小工具提供了一个多和不可编辑的对象,显示文本,自动断行和其内容的理由.   这个小工具提供了一个多和不可编辑的对象,显示文本,自动 ...

  8. django之中间件设置

    中间件 是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出 激活:添加到Django配置文件中的MIDDLEWARE_CLASSES元组中 每个中间件 ...

  9. Rhythmk 一步一步学 JAVA(7): jsp 自定义标签

    1.实现Tag接口: TagSupport类实现了Tag接口,为我们提供了4个重要的方法(见表6-5). 1.1. TagSupport类中的常用方法           int doStartTag ...

  10. halcon连续采集图像

    dev_close_window()dev_update_window('off')create_bar_code_model ([], [], BarCodeHandle)dev_open_wind ...