Modal弹出底层实现原理
2023-01-03 本文已影响0人
飘摇的水草
Modal弹出的底层实现原理,就是利用了一个 view 的动画,并且加在了 window 上而已
下面是 Modal 弹出的
@implementation ViewController
- (IBAction)didclicked:(id)sender
{
SecondViewController *secondCon = [[SecondViewController alloc]init];
secondCon.view.backgroundColor = [UIColor redColor];
self.secondVC = secondCon;
// [self.navigationController presentViewController:secondCon animated:YES completion:nil];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:secondCon.view];
secondCon.view.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
[UIView animateWithDuration:0.25 animations:^{
secondCon.view.transform = CGAffineTransformIdentity;
}];
}
@end
这是 dismiss 动画的
@implementation SecondViewController
- (IBAction)dismissClick:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(self.view.frame.origin.x, self.view.frame.size.height);
}];
}
@end