自定义弹出模态视图

来源:互联网 发布:大数据人才缺口 编辑:程序博客网 时间:2024/04/28 18:10

(1)设置根控制器

(2)设置主视图控制器

RootViewController.m

#import "ModelViewController.h"#import "UIViewController+MypresentModal.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];        self.view.backgroundColor = [UIColor greenColor];     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button.frame = CGRectMake(90, 90, 90, 50);    button.backgroundColor = [UIColor redColor];    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    }- (void)buttonAction {    ModelViewController *modelCtrl = [[ModelViewController alloc] init];        [self myPresentViewController:modelCtrl];    }

(3)设置模态视图控制器

ModelViewController.m

#import "UIViewController+MypresentModal.h"@interface ModelViewController ()@end@implementation ModelViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button.frame = CGRectMake(90, 90, 90, 50);    button.backgroundColor = [UIColor redColor];        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];    }- (void)buttonAction {        [self myDismissViewController];    }

(4)添加类目

UIViewController+MypresentModal.h

//定义弹出的动画效果- (void)myPresentViewController:(UIViewController *)viewController;//自定义关闭模态视图的动画效果- (void)myDismissViewController;
UIViewController+MypresentModal.m

//定义弹出的动画效果- (void)myPresentViewController:(UIViewController *)viewController {    //viewController是模态视图控制器        //设置模态视图的frame    viewController.view.frame = CGRectMake(320, 0, 320, 480);    //取得当前的主window    UIWindow *window = [UIApplication sharedApplication].keyWindow;    //将视图控制器的视图添加到window上显示    [window addSubview:viewController.view];    //开始动画    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:.3];        self.view.frame = CGRectMake(-320, 0, 320, 480);    viewController.view.frame = CGRectMake(0, 0, 320, 480);        //关闭动画    [UIView commitAnimations];        [self addChildViewController:viewController];        }//自定义关闭模态视图的动画效果- (void)myDismissViewController {    UIWindow *window = [UIApplication sharedApplication].keyWindow;        //开始动画    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:.3];    self.view.frame = CGRectMake(320, 0, 320, 480);    //得到模态视图控制器    window.rootViewController.view.frame = CGRectMake(0, 0, 320, 480);        [UIView commitAnimations];        [self removeFromParentViewController];    }



0 0
原创粉丝点击