QMUI_iOS
QMUI_iOS copied to clipboard
QMUIModalPresentationViewController 重复执行显示过程
Bug 表现
QMUIModalPresentationViewController使用showInView:vc.view方式弹出后,vc再次presentViewControllerB, 然后Bdismiss时QMUIModalPresentationViewController会再次执行一次显示过程- 前提条件是
B.modalPresentationStyle为UIModalPresentationCurrentContextUIModalPresentationFullScreen时才会有这种情况发生
截图
如何重现
- 如下代码 或 参见附件
- (IBAction)buttonAction:(UIButton *)sender {
TDXDTicketWalletDetailTipsView *tipsView = [[TDXDTicketWalletDetailTipsView alloc] init];
tipsView.backgroundColor = UIColor.yellowColor;
tipsView.layer.cornerRadius = 8;
tipsView.tips = @"使用该票品需要点击入场,选择入场方式点击使用该票品需要点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式使用该票品需要点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式使用该票品需要点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式点击入场,选择入场方式";
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.backgroundColor = UIColor.whiteColor;
[button addTarget:self action:@selector(tipsViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[tipsView addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(tipsView.mas_centerX);
make.centerY.mas_equalTo(tipsView.mas_centerY);
}];
self.modalVc = [[QMUIModalPresentationViewController alloc] init];
self.modalVc.contentView = tipsView;
self.modalVc.animationStyle = QMUIModalPresentationAnimationStylePopup;
self.modalVc.layoutBlock = ^(CGRect containerBounds, CGFloat keyboardHeight, CGRect contentViewDefaultFrame) {
[tipsView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(tipsView.superview.mas_centerX);
make.centerY.mas_equalTo(tipsView.superview.mas_centerY);
}];
};
[self.modalVc showInView:self.view animated:YES completion:nil];
}
- (void)tipsViewButtonAction:(UIButton *)button {
SecondViewController *vc = [SecondViewController new];
// UIModalPresentationCurrentContext UIModalPresentationFullScreen
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
}
预期的表现
- 不应该再次执行一次
显示过程
其他信息
- 设备: [模拟器、iPhone]
- iOS 版本: [iOS 14.4]
- Xcode 版本: [Xcode 12.5.1]
- QMUI 版本: [4.2.3] testststxxx.zip
问题原理分析:
QMUIModalPresentationViewController 的显示动画事件是在 viewWillAppear 内的.这就会导致一个问题.如果在进入下级页面之后.下级页面被销毁消失. QMUIModalPresentationViewController 重新显示.就会重新触发 viewWillAppear 方法.重新进入显示动画的逻辑.也就是说.不只是presentViewController 其实pushViewController 也会出现这个问题.
解决方案:
第一种, 在外部重写 showingAnimation 和 hidingAnimation 外部自行通过临时变量实现的需求
第二种,则需要再内部通过临时变量判断 在 showingAnimationWithCompletion 方法中拦截判断当前是否已经执行过一次显示动画
@xixisplit 当通过showInView时应该由框架内部保证不会被重复执行,比较合理吧?