通过删除一个UIView创建幻灯片动画,然后添加另一个UIView
问题描述:
我有视图,有一个webview和一个UIToolbar。通过删除一个UIView创建幻灯片动画,然后添加另一个UIView
我想创建一个动画,执行“kCATransitionFromTop”转换,方法是首先删除先前的webview,然后将新的webView添加到superView。 在此过渡期间,我希望工具栏保留现在的位置,而不是移动。
代码: 添加工具栏
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.tintColor = [UIColor blackColor];
[toolbar setFrame:CGRectMake(0, 480 - 88 - 20, 320.0, 44)];
//Add the toolbar as a subview to the navigation controller.
[self.view addSubview:toolbar];
动画:
UIView *currentView = webView;
UIView *theWindow = [currentView superview];
UIView *newView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, 320, self.view.bounds.size.height - self.navigationController.toolbar.frame.size.height)];
newView.backgroundColor = [UIColor whiteColor];
// remove the current view and replace with new view
[currentView removeFromSuperview];
[theWindow insertSubview:newView belowSubview:toolbar];
// set up the animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.4];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromTop];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:@"PushNextArticleView"];
一切似乎摸出OK,工具栏除了与web视图移动,以及... 我是怎么做错了,我该如何解决这个问题?
答
难道你不是故意只适用于的转换newView
?
[[newView layer] addAnimation:animation forKey:@"PushNextArticleView"];
谢谢,解决了这个问题;然而,过渡之间现在有一个黑洞。我想removeFromSuperview不是被动的吗? – user959974 2011-12-20 21:03:32
正确。将自己设置为图层的委托并实现'-animationDidStop:finished:'。这将在动画停止时发送消息,然后您可以从其超级视图中删除原始视图。 – 2011-12-20 21:09:01
当我注释掉[currentView removeFromSuperview]; ,不知何故,视图现在拒绝执行动画。看来他们不喜欢insertSubview方法..如果我告诉它AddSubview,动画将会很好..... – user959974 2011-12-20 21:36:08