美文网首页#iOS#HeminWon牛叉的demo上海恩美路演
iOS - 自定义弹框内容(两种方法)

iOS - 自定义弹框内容(两种方法)

作者: Mr_Bob_ | 来源:发表于2016-07-20 15:07 被阅读1976次
Untitled.gif
前言:

在开发会遇到众多自定义弹框的需求,如果是用到的情况非常多的话,就有有必要创建一个跟控制器,再根控制器中实现弹框的方法,如果是用的就一两个页面的话可以在在弹框页面下,实现即可,下面给出具体实现方法

第一种方法(在用到的情况比较少的情况下可以在当前页面这样写)
#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));
    
    // 创建背景的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];
            
        }
    }];
}
第二种方法(适合弹框在项目中用到地方非常多)
  • 思路:可以创建个跟控制器,然后以后用到的页面都继承于这个跟控制器,实现相应的方法即可实现弹框蒙版效果
  • 创建跟控制器代码如下:
#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
  • 以后所有用到的弹框的控制器就可以这样实现:
    首先要继承于跟控制器
#import <UIKit/UIKit.h>
#import "KFBaseViewController.h"
@interface TestViewController : KFBaseViewController
@end

然后在.m文件中实现如下:


#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;
    }];
}

相关文章

网友评论

    本文标题:iOS - 自定义弹框内容(两种方法)

    本文链接:https://www.haomeiwen.com/subject/hoevjttx.html