iOS模仿微信支付寶等底部彈出框

*****最開始很喜歡蘋果的白色彈出框揣苏,很是精致,但是在實際軟件使用中發(fā)現(xiàn)自帶彈窗啟動有些慢件舵,而且發(fā)現(xiàn)微信卸察、支付寶、網(wǎng)易云等都用的是另外一種彈窗铅祸,即下圖坑质,

IMG_1154.PNG

慢慢的也覺得這種不錯,于是仿造一個临梗,不好處請見諒涡扼。*****

彈窗用起來特別簡單,比如
MySheetView *view = [MySheetView new];
    view.title = @"退出后不會刪除任何歷史數(shù)據(jù)盟庞,下次登錄依然可以使用本賬號吃沪。" ;
//是否第一個按鈕為紅色,比如退出的按鈕
    view.haveRedButton = YES;
//按鈕名字數(shù)組茫经,從上到下巷波,不用寫取消按鈕,直接寫入了
    view.buttonNameArray = @[@"退出登錄"];

//處理事件卸伞,這里block返回了按鈕的title抹镊,可以直接判斷是哪個按鈕
    [view handleMyBlock:^(UIButton *selectButton,NSString *title) {
        
        if ([title isEqualToString:@"退出登錄"]) {
            [HGTools signOut];
            
            UINavigationController *login = [STORYBOARD instantiateViewControllerWithIdentifier:@"LOGNAVIC"];
            
            //動畫轉(zhuǎn)場
            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:@"關(guān)閉" 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:@"關(guān)閉" otherButtonTitles:nil];
                [alert show];
            }
            

        }
        
        
    }];
    
    
    [view show:self];

屏幕錄制2.gif

****用法歸結(jié)起來****

    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);//參數(shù)為選中的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;//下半部分區(qū)域高度
    
}
@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];
        
        
    }
    
    
    //標題不為空再創(chuàng)建標題
    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末终佛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子雾家,更是在濱河造成了極大的恐慌铃彰,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芯咧,死亡現(xiàn)場離奇詭異牙捉,居然都是意外死亡竹揍,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門邪铲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芬位,“玉大人,你說我怎么就攤上這事带到∶恋铮” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵揽惹,是天一觀的道長被饿。 經(jīng)常有香客問我,道長永丝,這世上最難降的妖魔是什么锹漱? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任箭养,我火速辦了婚禮慕嚷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘毕泌。我一直安慰自己喝检,他們只是感情好,可當我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布撼泛。 她就那樣靜靜地躺著挠说,像睡著了一般。 火紅的嫁衣襯著肌膚如雪愿题。 梳的紋絲不亂的頭發(fā)上损俭,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天,我揣著相機與錄音潘酗,去河邊找鬼杆兵。 笑死,一個胖子當著我的面吹牛仔夺,可吹牛的內(nèi)容都是我干的琐脏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼缸兔,長吁一口氣:“原來是場噩夢啊……” “哼日裙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起惰蜜,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤昂拂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后抛猖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體格侯,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡路克,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了养交。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片精算。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖碎连,靈堂內(nèi)的尸體忽然破棺而出灰羽,到底是詐尸還是另有隱情,我是刑警寧澤鱼辙,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布廉嚼,位于F島的核電站,受9級特大地震影響倒戏,放射性物質(zhì)發(fā)生泄漏怠噪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一杜跷、第九天 我趴在偏房一處隱蔽的房頂上張望傍念。 院中可真熱鬧,春花似錦葛闷、人聲如沸憋槐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阳仔。三九已至,卻和暖如春扣泊,著一層夾襖步出監(jiān)牢的瞬間近范,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工延蟹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留评矩,地道東北人。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓等孵,卻偏偏與公主長得像稚照,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子俯萌,可洞房花燭夜當晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,081評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫果录、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,095評論 4 62
  • 撒哈拉的故事
    勿念勿忘閱讀 278評論 0 1
  • 第一次認識羽泉咐熙,還是小時候在看快樂大本營的時候弱恒,那時哥倆留著一頭殺馬特發(fā)型,在節(jié)目中唱著成名曲《最美》棋恼。當時作為一...
    回頭滄海閱讀 401評論 3 3
  • 早上六點鐘,迎著刺骨的寒風(fēng)义起,和室友一起走向公交站拉背,期間遇到了軍訓(xùn)時的教官,現(xiàn)在稱作學(xué)長默终,畢竟只比我們大一屆椅棺,學(xué)長和...
    橘子味的冬天閱讀 186評論 0 0