*****最開始很喜歡蘋果的白色彈出框揣苏,很是精致,但是在實際軟件使用中發(fā)現(xiàn)自帶彈窗啟動有些慢件舵,而且發(fā)現(xiàn)微信卸察、支付寶、網(wǎng)易云等都用的是另外一種彈窗铅祸,即下圖坑质,
慢慢的也覺得這種不錯,于是仿造一個临梗,不好處請見諒涡扼。*****
彈窗用起來特別簡單,比如
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];
//效果見下
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];
****用法歸結(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