有时候我们在开发中,系统提供的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. Android中关于JNI 的学习(五)在C文件里使用LogCat

    Log是开发过程中.对于我们调试程序非常重要的一个工具,有非常多时候,我们正是通过Log才干够看清楚程序是不是真的依照我们想像中的模式在跑,从而定位到问题所在的地方.而在Android开发中,毫无疑问 ...

  2. yii2自定义500错误

    由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式 看了这篇博客,知道了如何去自定义自己错误页面 : http://t ...

  3. js中去掉字符串中的某个指定字符

    假设一个data里面的数据是[tian,12],现在去掉[],代码如下 data=data.replace("[",""); data=data.replace ...

  4. Proxool抛出的警告 was active for 365172 milliseconds and has been removed automaticaly

    WARN cetDB:149 - #0005 was active for 365172 milliseconds and has been removed automaticaly. The Thr ...

  5. C++:const_cast类型转换

    针对const_cast,太多人在用同一个示例问同一个问题:void main(){   const int a = 3;   const int *pc = &a;   int *p = c ...

  6. maven release plugin插件

    1.打包版本区别 SNAPSHOT 快照版本(开发阶段,不稳定,容易出现bug)RELEASE 正式版本(外部依赖使用阶段,稳定,很少出现bug)Tag :标记每次代码提交的版本(比较稳定,类似分支) ...

  7. 05——wepy框架中的一些细节

    1.wepy组件的编译 wepy中使用一个组件时候,需要先引用(import).再在需要使用该组件的页面(或组件)中声明.例如: import Counter from '/path/to/Count ...

  8. 搭建psdash 监控系统

    一.监控系统介绍 Psdash 是一款查看 Linux 系统信息的 web 面板,和另一款系统监控工具 Glances 一样,psDash 的系统信息的采集也是由 psutil 完成的.和 Glanc ...

  9. 第二章:Android Studio概述(二)[学习Android Studio汉化教程]

    The Main Menu Bar 主菜单栏  主菜单栏位于Android Studio的最上面,你几乎可以利用主菜单和其子菜单来执行任何操作.不像Android Studio中其他的一些菜单,主菜单 ...

  10. Python,OpenGL生命游戏

    初学Python和OpenGL,练手的第一个小程序life.py,这个小程序在日后会不断调整,增加类.优化判断及操作 执行效果: 按正规生命游戏的规则: 1.周围生命等于3时产生生命 2.周围生命等于 ...