仿微信-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 ...
随机推荐
- FastAdmin 如何升级?
FastAdmin 如何升级? 官方推荐使用 git 升级 FastAdmin. 升级 FastAdmin 核心代码 git stash git pull git stash pop 更新前端组件 比 ...
- python官网
https://www.python.org/ https://docs.python.org/2/library/pydoc.html
- LNMP.ORG 安装LNMP
参考:http://lnmp.org/install.html cd /usr/local/src wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.ta ...
- Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp
http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...
- Python Socke
回射 SERVER #!/usr/bin/python3 #_*_ coding:utf- _*_ import socket,os,time import socketserver import t ...
- cocos2dx 3.0 编译工程
下载完2dx,运行setup.py,参考设置ANDROID_SDK,NDK_ROOT,ANT_ROOT变量 创建工程 cocos new testGame -p com.game.test -l cp ...
- ContentProvider用法
1.通过Context的getContentRsolver()获取ContentResolver类的实例. 2.ContentResolver中接收的不是表明而是内容URI 3.解析内容URI获得Ur ...
- Spring Framework中常见的事务传播陷阱(译文)
最近看到Medium上一篇讨论Spring Framework中事务传播的文章,解释了几种常见的问题,解释的不错,这里直接翻译吧(意译为主,粗体和斜体是我自己加上的). 译文: 这是我的第一篇文章,我 ...
- Mac OS Virtualbox 倒入 ova 镜像文件
原文:https://www.zhihu.com/question/40785995 ------------------ 自己亲测 --------------------- 把 windows 系 ...
- Timesten 日常管理命令合集
Timesten 日常管理命令合集 以下所有操作都是基于TT 11 版,早前版本本人没用过,命令是否适用我不清楚啊! 各类服务管理 一.TT的启停 停服务: 1.停止复制与cache 进程: ...