iOS模仿微信支付宝等底部弹出框

*****最开始很喜欢苹果的白色弹出框,很是精致,但是在实际软件使用中发现自带弹窗启动有些慢,而且发现微信、支付宝、网易云等都用的是另外一种弹窗,即下图,

IMG_1154.PNG

慢慢的也觉得这种不错,于是仿造一个,不好处请见谅。*****

弹窗用起来特别简单,比如
MySheetView *view = [MySheetView new];
    view.title = @"退出后不会删除任何历史数据,下次登录依然可以使用本账号。" ;
//是否第一个按钮为红色,比如退出的按钮
    view.haveRedButton = YES;
//按钮名字数组,从上到下,不用写取消按钮,直接写入了
    view.buttonNameArray = @[@"退出登录"];

//处理事件,这里block返回了按钮的title,可以直接判断是哪个按钮
    [view handleMyBlock:^(UIButton *selectButton,NSString *title) {
        
        if ([title isEqualToString:@"退出登录"]) {
            [HGTools signOut];
            
            UINavigationController *login = [STORYBOARD instantiateViewControllerWithIdentifier:@"LOGNAVIC"];
            
            //动画转场
            CATransition *transition=[[CATransition alloc]init];
            
            transition.type=@"oglFlip";
            transition.subtype=kCATransitionFromLeft;
            
            transition.duration=0.5f;
            [self.view.window.layer addAnimation:transition forKey:@"HGTransitionAnimation"];
            
            self.view.window.rootViewController = login;
        }
        
    }];
    
    [view show:self];

//效果见下

屏幕录制2.gif
 MySheetView *view = [MySheetView new];

    NSArray *nameArray = @[@"拍照",@"从相册选择"];
    view.buttonNameArray = nameArray;
    [view handleMyBlock:^(UIButton *selectButton, NSString *title) {
        
        if ([title isEqualToString:nameArray[0]]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                
                [self presentViewController:picker animated:YES completion:nil];
                
            }else{
                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                [alert show];
            }
        }
        
        if ([title isEqualToString:nameArray[1]]) {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
                
                [self presentViewController:picker animated:YES completion:nil];
                
            }else{
                UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相册不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                [alert show];
            }
            

        }
        
        
    }];
    
    
    [view show:self];

屏幕录制2.gif

****用法归结起来****

    MySheetView *view = [MySheetView new];

    view.title = @"标题";
    view.buttonNameArray = @[@"按钮1",@"按钮2"];
    [view handleMyBlock:^(UIButton *selectButton, NSString *title) {
        //处理按钮事件
         if(title isEqualToString:@"按钮1"){
           // do something
          }
   }];
    
    
    [view show:self];
.h文件
//
//  MySheetView.h
//  EPBoxChannelApp
//
//  Created by koreadragon on 2016/12/14.
//  Copyright © 2016年 koreadragon. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^selectBlock)(UIButton *selectButton,NSString *title);//参数为选中的button

@interface MySheetView : UIView



//弹出
-(void)show:(UIViewController *)vc;

//按钮集合
@property(nonatomic,strong)NSArray *buttonNameArray;
//标题
@property(nonatomic,copy)NSString*title;

@property(nonatomic,copy)selectBlock myBlock;
//是否第一个为红色按钮
@property(nonatomic,assign)BOOL haveRedButton;
//处理按钮事件
-(void)handleMyBlock:(selectBlock)block;

@end

.m文件

//
//  MySheetView.m
//  EPBoxChannelApp
//
//  Created by koreadragon on 2016/12/14.
//  Copyright © 2016年 koreadragon. All rights reserved.
//

#import "MySheetView.h"


#define MYRGB(r,g,b,a)  [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:(a)]
static float font = 13.0;

@interface MySheetView (){
    
    UIView *whiteView;
    UIView *blackView;//上下遮罩
    
    float whiteY;//下半部分区域高度
    
}
@end



@implementation MySheetView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(instancetype)initWithFrame:(CGRect)frame{
    UIWindow *window =  [[UIApplication sharedApplication].delegate window];
    
  
    if (self = [super initWithFrame:window.bounds]) {
        
//        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];

       
        
    }
    
    return self;
}


-(void)show:(UIViewController *)vc{
    
    UIWindow *window =  [[UIApplication sharedApplication].delegate window];
    CGFloat width = window.frame.size.width;
    CGFloat height = window.frame.size.height;
    
    NSInteger count = self.buttonNameArray.count;
    
    
    //增加上半部分遮罩
    CGRect rect = [HGTools fittingRectWithString:_title fontSize:font CGSize:CGSizeMake(width * 0.8, MAXFLOAT)];
    
    
    CGFloat realHeight;
    if (rect.size.height == 0) {//标题为空
        realHeight = 0;
    }else{
        realHeight = rect.size.height > 50 ? rect.size.height : 50;
        
    }
    
    //60 为取消按钮的背景高,实高55
    CGFloat blackHeight = height - count*51-60 - realHeight;
    
    
    //如果有标题,就上移两个像素,以便展示标题,否则为0
    CGFloat inset;
    
    if (_title != 0) {
        inset = 15;
    }else{
        inset = 0;
    }
    
    blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, blackHeight-inset)];

     [self addSubview:blackView];
    //上半部分添加轻触事件,取消显示
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelSelect)];
    [blackView addGestureRecognizer:tap];

    
    
    //下半部分遮罩
    
    
    whiteY = blackHeight-inset;
    whiteView = [[UIView alloc]initWithFrame:CGRectMake(0,height, width, height-blackHeight+inset)];
    whiteView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];
    
    //按钮们
    for (int i = 0; i < count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        if (i == count-1) {//如果有需求,第一个按钮为红色
            if (_haveRedButton) {
                [button setTitleColor:MYRGB(219, 0, 0, 1.0) forState:UIControlStateNormal];
            }
        }
        
        [button setTitle:_buttonNameArray[count-1-i] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, whiteView.frame.size.height-60-(i+1)*51, width, 50);
        
        
        [button addTarget:self action:@selector(privateAction:) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:button];
        
        
    }
    
    
    //标题不为空再创建标题
    if (_title != nil) {
        
        //标题
        UILabel *label = [UILabel new];
        label.backgroundColor = [UIColor whiteColor];
        label.text = _title;
        label.textColor = [UIColor grayColor] ;
        label.textAlignment = NSTextAlignmentCenter;
        
        
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:font];
        label.userInteractionEnabled = YES;
        label.frame = CGRectMake(width*0.1, 7, width*0.8,realHeight+1 );
        
        UILabel *labelBack = [UILabel new];
        labelBack.backgroundColor = [UIColor whiteColor];
        labelBack.frame = CGRectMake(0, 0, width,realHeight+14 );
        
        [whiteView addSubview:labelBack];
        [whiteView addSubview:label];
    }
    
    //再添加一个取消按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, whiteView.frame.size.height- 55, width, 55);
    
    [button addTarget:self action:@selector(cancelSelect) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [whiteView addSubview:button];
    

    [window addSubview:self];

}


-(void)layoutSubviews{
    
   UIWindow *window =  [[UIApplication sharedApplication].delegate window];
    CGFloat width = window.frame.size.width;
    [super layoutSubviews];
    
    
      self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{

        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];

        whiteView.frame=CGRectMake(0,whiteY, width, whiteView.frame.size.height);
        
        [self addSubview:whiteView];
        
    } completion:^(BOOL finished) {
        

        
    }];
    
  
}


-(void)dismiss{
    UIWindow *window =  [[UIApplication sharedApplication].delegate window];
    CGFloat width = window.frame.size.width;
    CGFloat height = window.frame.size.height;
    
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        whiteView.frame=CGRectMake(0,height, width, whiteView.frame.size.height);
        
        self.backgroundColor = [UIColor clearColor];

    } completion:^(BOOL finished) {
        
        
        [self removeFromSuperview];
        
    }];
    
 
}



-(void)privateAction:(UIButton *)sender{
    
    
    if (self.myBlock) {
        
        self.myBlock(sender,sender.titleLabel.text);
    }
    [self dismiss];
    
}

-(void)handleMyBlock:(selectBlock)block{
    
    self.myBlock = block;
    
}

-(void)cancelSelect{
    
    [self dismiss];
    
}



@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容