【问题标题】:removeFromSuperview with animation and views management带有动画和视图管理的 removeFromSuperview
【发布时间】:2011-10-13 14:42:46
【问题描述】:

我对此进行了一些挖掘,但似乎没有什么能真正回答我的特定问题(甚至没有这个:Is it possbile to removeFromSuperview with Animation?)。

基本上,我的应用程序从一个欢迎屏幕开始,用户点击“登录”,然后进入登录视图,然后进入标签栏视图,这是实际的应用程序。

我这样做的方式是,我编写了一个自定义类 - TabBarController,它设置了所有选项卡及其各自的视图控制器。现在,当用户单击“登录”时,我正在调用 removeFromSuperview 并显示标签栏。

我正在尝试找到一种方法来为从登录页面到标签栏的过渡设置动画。我在这里尝试了一些建议的解决方案,但似乎没有一个可以完成这项工作。这是我在 signin.m 视图控制器中的代码。我正在寻找动画出当前视图(理想情况下,不仅仅是淡出,而是更酷的东西,如翻转等)。

//when done signing in --> go to the tab bar view 
-(IBAction)done:(id)sender {

TabBarController  *tabController = [[TabBarController alloc] init];
[UIView beginAnimations:@"removeWithEffect" context:nil];
[UIView setAnimationDuration:4.0];
self.parentViewController.view.frame = CGRectMake(0,0,320,480);
self.parentViewController.view.alpha = 1.0f;
[UIView commitAnimations];
[self.parentViewController.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.5f];
[self presentModalViewController:tabController animated:YES];

}

感谢任何帮助!

【问题讨论】:

    标签: iphone objective-c uiview view uitabbarcontroller


    【解决方案1】:

    那样做是行不通的。 presentModalViewController 在自己的视图上显示 viewController 的视图。它不会替换源 viewController (self)。 由于您从视图层次结构中删除了 self.parentViewController.view,因此它无法以模态方式显示您的 tabController,因为您已删除了 self。

    无论如何,我会向您推荐另一种实现视图布局的方法: 创建一个 tabBarViewController 并将其视图添加到 rootView(应用程序委托中的 self.window 或您现在使用的任何内容)。然后将您的登录视图添加到同一视图。由于视图层次结构,登录视图将显示在 tabBar.view 上方。并且完成按钮应该以这种方式实现:(我正在使用块语法来制作动画)

    -(IBAction)done:(id)sender {
        [UIView animateWithDuration:1.0 
                         animations:^{
                             self.view.frame = CGRectMake(0, 480, 320, 480);
                             self.view.alpha = 0.0
                         } 
                         completion:^(BOOL finished){
                             [self.view removeFromSuperView];
                         }
         ];
    }
    

    除了 alpha、大小或位置之外,您还可以为更多内容设置动画。只需查看documentation 中的动画即可。我想,你会对 view.transform 感兴趣以提交翻转动画。 ;)

    【讨论】:

    • +1 谢谢@Yinkou,这很有帮助。您是否介意展示一些有关如何执行此操作的代码:“创建一个 tabBarViewController 并将其视图添加到 rootView(应用程序委托中的 self.window 或您现在使用的任何内容)。然后将您的登录视图添加到同一个视图。 " - 不知道如何做到这一点。谢谢!
    • 我会给你写一个示例项目。告诉我怎么给你。
    【解决方案2】:

    这就是你必须在动画后删除视图的方法。

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelay:2.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:myView];
    [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
    
    [UIView commitAnimations];
    

    希望这会有所帮助。 编码愉快。

    【讨论】:

    • @TommyG 检查我的答案。它可能对你有用。
    • 无论持续时间长短,它都会立即淡出,并且导航栏仍然存在。另外,我不确定如何从这里添加我的下一个视图...?
    • 你想用视图的 alpha 属性淡化它吗?
    • 不,我从字面上复制粘贴了你放在这里的内容,只是添加了这些行来添加我的标签栏视图,但它似乎不起作用:TabBarController *tabController = [[TabBarController alloc] init ]; [self presentModalViewController:tabController Animation:YES];
    • 你可以改变动画块内view的alpha属性来产生淡入淡出的效果。试试吧。
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多