ios 使用UIwindow自制弹窗

创建一个简单的弹窗,先定义一个全局的uiwindow。

在使用时创建并覆盖全局大小。设置背景色的透明度,使用户可以看到下方,设置windowlevel小于警告框,设置window为显示。

    self.CartWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)];

    self.CartWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];

    self.CartWindow.windowLevel = UIWindowLevelAlert-10;

    self.CartWindow.layer.masksToBounds = YES;

    self.CartWindow.hidden=NO;


随后创建viewcontroller使之显示在window上

UIViewController *cartVC = [[UIViewController alloc]init]

在VC的返回事件中添加销毁事件

@Weakify(self);

cartVC.closeBlock = ^() {

@Strongify(self);

            [weakSelf dismissWindow];


    };

    self.CartWindow.rootViewController = cartVC;

    cartVC.view.frame=CGRectMake(0, Screen_height, Screen_width, Screen_height);


    添加弹出动画

    [UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{

        cartVC.view.frame=CGRectMake(0, 0, Screen_width, Screen_height);

    } completion:nil];



//销毁window

-(void)dismissWindow

{

    UIViewController *vc=self.CartWindow.rootViewController;

    self.CartWindow.backgroundColor=[UIColor clearColor];


@Weakify(self);

    [UIView animateWithDuration:0.3f animations:^{

        vc.view.frame=CGRectMake(0, Screen_height, Screen_width, Screen_height);

    } completion:^(BOOL finished) {

@Strongify(self);

        self.CartWindow.hidden = YES;

        self.CartWindow = nil;

    }];

}



//@Weakify(self)和@Strongify(self)为ReactiveCocoa里方法


在自定义的UIviewcontroller中设置背景色为透明,并添加一个覆盖真个view的Button,模仿点击灰色透明区域返回



————菜鸟

你可能感兴趣的:(ios 使用UIwindow自制弹窗)