我认为这是 iOS5 中的一个错误。我已经做了一些从其他模态呈现模态视图控制器的实验,似乎第二个模态永远不会使下面的屏幕变暗。我什至设置了一个测试项目,允许您彼此启动无尽的模态,并且似乎每个其他模态都没有像预期的那样变暗或阻止触摸。
UIWindow 子视图上的快速 NSLog 告诉我们,虽然适当地添加了阴影,但 dimmingView 没有。我正在研究一种方法来展示我自己的调光视图。找到方法后会更新此答案。
Window Subviews: (
"<UILayoutContainerView: 0xf63e3c0; frame = (0 0; 768 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf645640>>",
"<UIDimmingView: 0xf683990; frame = (0 0; 768 1024); opaque = NO; layer = <CALayer: 0xf6836d0>>",
"<UIDropShadowView: 0xf683130; frame = (64 64; 640 896); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf6831c0>>",
"<UIDropShadowView: 0x292110; frame = (74 242; 620 540); transform = [0, 1, -1, 0, 0, 0]; autoresize = LM+RM+TM+BM; layer = <CALayer: 0x292150>>"
)
解决方案二:
所以在我的动画外观的最终解决方案中。 我还开始考虑我的第一个解决方案,它在技术上可能会激怒 Apple 并导致拒绝,因为 UIDimmingView 没有记录,我们“触摸它”。我添加了一个背景颜色为 alpha 的 UIView我们想要我的viewController。然后我在呈现模态时对其进行动画处理,并在调用第二个模态的委托时反转动画。对我来说看起来很不错。可能会进行一些时间和 alpha 调整以使其恰到好处,但它可以正常工作并且看起来不错。
- (void)viewDidLoad
{
dim = [[UIView alloc] init];
[dim setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.35]];
[dim setAlpha:0.0];
[dim setUserInteractionEnabled:NO];
[self.view addSubview:dim];
}
- (void)presentModal
{
[self.view bringSubviewToFront:dim];
[dim setFrame:self.view.frame];
[UIView animateWithDuration:0.25 animations:^{
[dim setAlpha:1.0];
}];
}
- (void)modalDelegateFinished
{
[UIView animateWithDuration:0.25 animations:^{
[dim setAlpha:0.0];
}];
}
解决方案一:
好吧,这可行,但它没有我想要的动画效果。然而,它确实重用了已经存在的东西,所以这可能是一个加分项。
- (void)viewDidAppear:(BOOL)animated
{
// Add a gesture to dismiss the view if tapped outside.
UIGesture *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOutsideView:)];
[gesture setNumberOfTapsRequired:1];
[gesture setCancelsTouchesInView:NO];
[self.view.window addGestureRecognizer:gesture];
// Move the dimmingview to just below the dropshadow.
UIView *dim = [self.view.window.subviews objectAtIndex:1];
[self.view.window insertSubview:dim atIndex:2];
}
- (void)tappedOutsideView:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint location = [sender locationInView:nil];
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {
// remove the gesture on the window
[self.view.window removeGestureRecognizer:sender];
// Move the dimmingview back where it belongs
UIView *dim = [self.view.window.subviews objectAtIndex:2];
[self.view.window insertSubview:dim atIndex:1];
}
}
}
同样作为故障保护,它对于 viewDidDisappear 中的相同内容可能是一个好主意。我的完成按钮调用了 tappedOutside 视图,所以我知道手势和 dimmingView 总是正确的。但如果你不这样做,你可以把它放在 viewDidDisappear 中。
- (void)viewDidDisappear:(BOOL)animated
{
// remove the gesture on the window
for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
[self.view.window removeGestureRecognizer:gesture];
}
// Move the dimmingview back where it belongs
UIView *dim = [self.view.window.subviews objectAtIndex:2];
[self.view.window insertSubview:dim atIndex:1];
}