美文网首页
Modal弹出底层实现原理

Modal弹出底层实现原理

作者: 飘摇的水草 | 来源:发表于2023-01-03 17:28 被阅读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

相关文章

网友评论

      本文标题:Modal弹出底层实现原理

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