123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // ZCShareSheetView.m
- // XLChat
- //
- // Created by 张灿 on 2017/11/15.
- // Copyright © 2017年 张灿. All rights reserved.
- //
- #import "ZCShareSheetView.h"
- #define ZCShareSheetBtnHeight (40*KScreenWidth/320)//根据比例得到高度
- @implementation ZCShareActionBtn
- + (ZCShareActionBtn *)actionWithTitle:(NSString *)title image:(UIImage*)image andblock:(shareBtnBlock)block{
- ZCShareActionBtn *action = [[ZCShareActionBtn alloc]initWithTitle:title image:image andblock:block];
- return action;
- }
- - (id)initWithTitle:(NSString *)title image:(UIImage*)image andblock:(shareBtnBlock)block{
- if (self = [super init]) {
- self.block = block;
- self.title = title;
- self.image = image;
- }
- return self;
- }
- @end
- @interface ZCShareSheetView ()
- {
- UIView *_alertView;
- CGFloat _AllHeight;
- NSMutableArray *_marr;
- NSInteger _currentSelectIndex;
- NSMutableArray* _btnArray;
- }
- @end
- @implementation ZCShareSheetView
- - (UIWindow*)alertWindow{
- if (!_alertWindow) {
- _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- _alertWindow.windowLevel = UIWindowLevelStatusBar+1;
- }
- return _alertWindow;
- }
- - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray<ZCShareActionBtn *> *)arr column:(NSInteger)column{
- if (self = [super init]) {
- self.frame = [UIScreen mainScreen].bounds;
- _btnArray = [NSMutableArray array];
- _marr = [NSMutableArray arrayWithArray:arr];
- UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
- [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
-
- control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5];
- // control.alpha = 0.5;
-
- [self addSubview:control];
- _alertView = [[UIView alloc]init];
- _alertView.backgroundColor = [UIColor whiteColor];
- [self addSubview:_alertView];
- _currentSelectIndex = index;
- CGFloat btnWidth = (KScreenWidth-(column+1)*20.0)/column;
- // NSInteger row = arr.count/column+1;//行数
- NSInteger row = (NSInteger)ceilf(arr.count/(CGFloat)column);
- if (title) {
- if (show) {
- _AllHeight = ZCShareSheetBtnHeight+ZCShareSheetBtnHeight+row*(btnWidth+20)+(row+1)*20+SafeHeight;
- }else{
- _AllHeight = ZCShareSheetBtnHeight+row*(btnWidth+20)+(row+1)*20+SafeHeight;
- }
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCShareSheetBtnHeight)];
- titleLabel.text = title;
- titleLabel.textAlignment = NSTextAlignmentCenter;
- titleLabel.font = LCBFont17;
- titleLabel.textColor = LZ273145Color;
- [_alertView addSubview:titleLabel];
- UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, ZCShareSheetBtnHeight, KScreenWidth, row*(btnWidth+20)+(row+1)*20)];
- // btnBkgView.backgroundColor = [UIColor whiteColor];
- [_alertView addSubview:btnBkgView];
- for (int i = 0; i < arr.count; i++) {
- ZCShareActionBtn *action = arr[i];
- UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20+(i%column)*(btnWidth+20), 20+(i/column)*(20+20+btnWidth), btnWidth, btnWidth+20)];
- // [button setBackgroundImage:action.image forState:(UIControlStateNormal)];
- button.titleLabel.font = LCFont14;
- // button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, 0, 0, 0);
- button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
- button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, -btnWidth, 0, 0);
- [button setImage:action.image forState:(UIControlStateNormal)];
- [button setTitle:action.title forState:UIControlStateNormal];
- [button setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
- button.tag=i;
- [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
- [btnBkgView addSubview:button];
- [_btnArray addObject:button];
- }
- if (show) {
- UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
- cancel.frame = CGRectMake(0, ZCShareSheetBtnHeight+0+row*(btnWidth+20)+(row+1)*20, KScreenWidth, ZCShareSheetBtnHeight);
- [cancel setTitle:@"取消" forState:UIControlStateNormal];
- [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
- // cancel.backgroundColor = [UIColor whiteColor];
- [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
- [_alertView addSubview:cancel];
- [cancel addLineWithColor:LZF5F4F7Color lineRect:CGRectMake(16.0f, 0.0f, cancel.mj_w - 32.0f, 0.5f)];
- }
- }else{
- if (show) {
- _AllHeight = ZCShareSheetBtnHeight+10+row*(btnWidth+20)+(row+1)*20+SafeHeight;
- }else{
- _AllHeight = row*(btnWidth+20)+(row+1)*20+SafeHeight;
- }
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, row*(btnWidth+20)+(row+1)*20)];
- btnBkgView.backgroundColor = [UIColor whiteColor];
- [_alertView addSubview:btnBkgView];
- for (int i = 0; i < arr.count; i++) {
- ZCShareActionBtn *action = arr[i];
- UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20+(i%column)*(btnWidth+20), 20+(i/column)*(20+20+btnWidth), btnWidth, btnWidth+20)];
- // [button setBackgroundImage:action.image forState:(UIControlStateNormal)];
- button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
- button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, -btnWidth, 0, 0);
- button.titleLabel.font = [UIFont systemFontOfSize:15];
- [button setImage:action.image forState:(UIControlStateNormal)];
- [button setTitle:action.title forState:UIControlStateNormal];
- [button setTitleColor:LCTextNormal forState:UIControlStateNormal];
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
- button.tag=i;
- [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
- [btnBkgView addSubview:button];
- [_btnArray addObject:button];
- }
- if (show) {
- UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
- cancel.frame = CGRectMake(0, 0+row*(btnWidth+20)+(row+1)*20, KScreenWidth, ZCShareSheetBtnHeight);
- [cancel setTitle:@"取消" forState:UIControlStateNormal];
- [cancel setTitleColor:HexColorFromRGB(0x9F9DA5) forState:UIControlStateNormal];
- // cancel.backgroundColor = [UIColor whiteColor];
- [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
- [_alertView addSubview:cancel];
- [cancel addLineWithColor:[HexColorFromRGB(0x4F4B5B) colorWithAlphaComponent:0.3f] lineRect:CGRectMake(16.0f, 0.0f, cancel.mj_w - 32.0f, 0.5f)];
- }
-
- }
- [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:_alertView size:CGSizeMake(20, 20)];
-
- }
- return self;
- }
- - (void)youpaiftapClick:(UIButton *)button{
-
- ZCShareActionBtn *action = _marr[button.tag];
- [self hide];
- //加个延时操作防止先回调再hide,影响用户体验
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if (action.block) {
- action.block(action.title);
- }
- });
- }
- - (void)show{
- [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
- _alertWindow.hidden = NO;
- [_alertWindow addSubview:self];
- [UIView animateWithDuration:0.3 animations:^{
- _alertView.frame = CGRectMake(0, KScreenHeight-_AllHeight, KScreenWidth, _AllHeight);
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)hide{
- [UIView animateWithDuration:0.3 animations:^{
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
-
- _alertWindow.hidden = YES;
- _alertWindow = nil;
- }];
- }
- @end
|