Untitled.gif
前言:
在開發(fā)會遇到眾多自定義彈框的需求,如果是用到的情況非常多的話,就有有必要創(chuàng)建一個跟控制器,再根控制器中實現(xiàn)彈框的方法,如果是用的就一兩個頁面的話可以在在彈框頁面下,實現(xiàn)即可,下面給出具體實現(xiàn)方法
第一種方法(在用到的情況比較少的情況下可以在當前頁面這樣寫)
#import "ViewController.h"
#import "GiveScoreView.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
{
GiveScoreView *giveScoreView;
UIView *bg;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)popCustomView:(id)sender {
// 自定義彈框內容
giveScoreView = [[[NSBundle mainBundle] loadNibNamed:@"GiveScoreView" owner:self options:nil] lastObject];
giveScoreView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(giveScoreView.bounds));
// 創(chuàng)建背景的view
bg = [[UIView alloc] initWithFrame:self.view.bounds];
bg.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:0];
[bg addSubview:giveScoreView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
[bg addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
[UIView animateWithDuration:.35 animations:^{
bg.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:0.4];
[UIView animateWithDuration:.35 animations:^{
[giveScoreView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-giveScoreView.frame.size.height, giveScoreView.frame.size.width, giveScoreView.frame.size.height)];
}];
} completion:^(BOOL finished) {
}];
[self showInView:self];
}
- (void)showInView:(UIViewController *)Sview
{
[Sview.view addSubview:bg];
}
-(void)tappedCancel{
[UIView animateWithDuration:.35 animations:^{
[giveScoreView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds),CGRectGetWidth(self.view.bounds), 0)];
bg.alpha = 0;
} completion:^(BOOL finished) {
if (finished) {
[bg removeFromSuperview];
}
}];
}
第二種方法(適合彈框在項目中用到地方非常多)
- 思路:可以創(chuàng)建個跟控制器,然后以后用到的頁面都繼承于這個跟控制器,實現(xiàn)相應的方法即可實現(xiàn)彈框蒙版效果
- 創(chuàng)建跟控制器代碼如下:
#import <UIKit/UIKit.h>
@interface KFBaseViewController : UIViewController
@property (nonatomic,strong)UIView *moonlightView;
- (void)tapMoonview:(UITapGestureRecognizer *)tap;
@end
#import "KFBaseViewController.h"
#import "AppDelegate.h"
@interface KFBaseViewController ()
@end
@implementation KFBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.modalPresentationCapturesStatusBarAppearance = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self createBgView];
}
- (void)createBgView{
_moonlightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_moonlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:_moonlightView];
_moonlightView.hidden = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMoonview:)];
self.moonlightView.userInteractionEnabled = YES;
tap.numberOfTapsRequired =1;
[_moonlightView addGestureRecognizer:tap];
}
- (void)tapMoonview:(UITapGestureRecognizer *)tap{
self.moonlightView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 以后所有用到的彈框的控制器就可以這樣實現(xiàn):
首先要繼承于跟控制器
#import <UIKit/UIKit.h>
#import "KFBaseViewController.h"
@interface TestViewController : KFBaseViewController
@end
然后在.m文件中實現(xiàn)如下:
#import "TestViewController.h"
#import "GiveScoreView.h"
#import "AppDelegate.h"
@interface TestViewController ()
{
GiveScoreView *giveScoreView;
UIView *bg;
}
@end
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)popAction:(id)sender {
self.moonlightView.hidden = NO;
// 自定義的彈框樣式
giveScoreView = [[[NSBundle mainBundle] loadNibNamed:@"GiveScoreView" owner:self options:nil] lastObject];
giveScoreView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds)- CGRectGetHeight(giveScoreView.bounds) , CGRectGetWidth(self.view.bounds), CGRectGetHeight(giveScoreView.bounds));
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:giveScoreView];
giveScoreView.alpha = 0;
[UIView animateWithDuration:0.3 animations:^{
giveScoreView.hidden = NO;
giveScoreView.alpha = 1;
}];
}
- (void)tapMoonview:(UITapGestureRecognizer *)tap{
[super tapMoonview:tap];
[self closeAlerViwe];
}
- (void)closeAlerViwe{
[UIView animateWithDuration:0.3 animations:^{
giveScoreView.hidden = YES;
giveScoreView.alpha = 0;
[giveScoreView removeFromSuperview];
self.moonlightView.hidden = YES;
}];
}