仿微信-ActionSheet
有时候我们在开发中,系统提供的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的更多相关文章
- iOS开发-仿微信图片分享界面实现
分享功能目前几乎已成为很多app的标配了,其中微信,微博等app的图片分享界面设计的很棒,不仅能够展示缩略图,还可以预览删除.最近我在做一款社交分享app,其中就要实现图文分享功能,于是试着自行实现仿 ...
- Nuxt+Vue聊天室|nuxt仿微信App界面|nuxt.js聊天实例
一.项目简述 nuxt-chatroom 基于Nuxt.js+Vue.js+Vuex+Vant+VPopup等技术构建开发的仿微信|探探App界面社交聊天室项目.实现了卡片式翻牌滑动.消息发送/emo ...
- Android -- 真正的 高仿微信 打开网页的进度条效果
(本博客为原创,http://www.cnblogs.com/linguanh/) 目录: 一,为什么说是真正的高仿? 二,为什么要搞缓慢效果? 三,我的实现思路 四,代码,内含注释 五,使用方法与截 ...
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- 小程序用户反馈 - HotApp小程序统计仿微信聊天用户反馈组件,开源
用户反馈是小程序开发必要的一个功能,但是和自己核心业务没关系,主要是产品运营方便收集用户的对产品的反馈.HotApp推出了用户反馈的组件,方便大家直接集成使用 源码下载地址: https://gith ...
- GSD_WeiXin(高仿微信)应用源码
高仿微信计划:已经实现功能 1.微信首页(cell侧滑编辑.下拉眼睛动画.下拉拍短视频.点击进入聊天详情界面) 2.通讯录(联系人字母排序.搜索界面) 3.发现(朋友圈) 4.我(界面) 待实现功能( ...
- Android仿微信拍摄短视频
近期做项目需要添加上传短视频功能,功能设置为类似于微信,点击开始拍摄,设置最长拍摄时间,经过研究最终实现了这个功能,下面就和大家分享一下,希望对你有帮助. 1.视频录制自定义控件: /** * 视频播 ...
- CSS3 仿微信聊天小气泡
今天给大家分享一个我刚做的项目中的一个小案例, 因为我们在做一个聊天的功能,之前的聊天页面UI很丑,我就不在这里展示给大家了. 现在就教大家怎么用css3制作一个和微信聊天界面一样的页面. 首先给大家 ...
- jquerymobile仿微信 - 01
jquerymobile仿微信 - 01 jquerymobile的组件感觉不咋地哇 本地调试最好是开一个web server,不然数据访问会有问题 <div data-role="p ...
随机推荐
- docker 镜像导入导出[转]
0)查看镜像id sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE quay.io/calico/node v1.0.1 c70511a4 ...
- Matlab 之 FFT的理解和应用
网上看了一些大牛的关于FFT的见解,加上自己的一点儿理解,针对以下这几个问题来加深对FFT的理解. 不知道大家有没有类似以下几点的困惑: 问题的提出 对于1秒钟输出的连续信号,使用采样率Fs不同,就会 ...
- js动态的属性名如何取该属性的值
var cls={cn1:"nihao",cn2="made",cn3="shuide"};var index=2;//可变获取cn2的值 ...
- Python——深浅Copy
1. 赋值 赋值:指向同一块内存地址,所以同时改变 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3 ...
- TCP 、UDP、IP包的最大长度
1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...
- C程序花括号嵌套层次统计(新)
[问题描述] 编写程序,统计给定的C源程序中花括号的最大嵌套层次,并输出花括号嵌套序列,该程序没有语法错误. 注意:1)源程序注释(/* … */)中的花括号应被忽略,不参与统计.2)源程序中的字符串 ...
- python 线程/进程模块
线程的基本使用: import threading # ###################### 1.线程的基本使用 def func(arg): print(arg) t = threading ...
- Maven和Gradle的比较
Gradle和Maven都是项目构建工具,但是完全是两个产品,maven应该目前在java企业级开发中占的比重比较大,Gradle是后起之秀,Google的Android Stadio主推的就是Gra ...
- EasyUI treegrid 删除一条或多条记录
function del_dg() { $.messager.defaults = { ok: "是", cancel: "否" }; var node = $ ...
- PyQt 5事件和信号
信号槽Signals & slots sld.valueChanged.connect(lcd.display) # 将滚动条的valueChanged信号连接到lcd的display插槽 # ...