第十九章 Modal
除了push之外,还有另外一种控制器的切换方式,那就是Modal
任何控制器都能通过Modal的形式展示出来
Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止
- 以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
- 关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;
Modal的主要实现就是在主窗口上添加一个新的View。原来的控制器的view消失,但并没有彻底移除。因为是一个强引用保存着,我们可以自己用代码实现Modal
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
JXViewController * vc = [[JXViewController alloc] init];
vc.view.backgroundColor = [UIColor orangeColor];
// 模仿Modal
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[window addSubview:vc.view];
vc.view.transform = CGAffineTransformMakeTranslation(0, vc.view.bounds.size.height);
[UIView animateWithDuration:1 animations:^{
vc.view.transform = CGAffineTransformIdentity;
}];
// 这里添加强引用,如果不引用的话这个方法之后vc就会死掉,虽然页面依然会有显示,但是不会响应任何操作
self.vc = vc;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:1 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
}];
}