【问题标题】:Transition right to left and back using CATransition?使用 CATransition 从右到左并返回?
【发布时间】:2011-10-25 12:40:57
【问题描述】:

我有一个由五个 uiviewcontroller 组成的 uitabbarcontroller。

当我在第一个 uiviewcontroller 的视图中点击一个按钮时,我希望隐藏 uiviewcontroller 并添加一个新的 uiviewcontroller,并从右到左过渡。我已经实现了这一点,但是当我点击新视图上的按钮时,应用程序崩溃了!!!该按钮没有附加代码。我收到 EXEC_BAD_ACCESS 错误。

有谁知道为什么会发生这种情况以及如何用新视图克服这个问题?

当我尝试模态视图转换时,视图功能正常。当我尝试使用 CATransition 时会发生这种情况。我需要 CATransition,因为 modalview 转换没有从右到左或从左到右的转换!

任何帮助表示赞赏!

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    这是在 tabviewcontroller 的 Uiview 和新的 uiview 之间进行转换所需的代码。代码转到一个按钮的 IBAction,它存在于 tabviewcontroller 的一个 uiview 中。

    //初始化应用委托

    AppDelegate appDelegate = (AppDelegate) [[UIApplication sharedApplication] 委托];

    // 获取当前显示的视图

    UIView *currentView = self.view;

    // 获取底层的UIWindow,或者包含当前视图视图的视图

    UIView *theWindow = [currentView superview];

    // remove the current view and replace with mynewView1
    [currentView removeFromSuperview];
    

    //将新视图分配给viewcontroller1。它是uitabbarcontroller的第一个uiviewcontroller。我的 uitabbarcontroller 在我的项目中由五个 uiviewcontroller 组成。 该按钮存在于第一个 viewcontroller1 中,这就是我将新视图分配给 viewcontroller1 的原因。如果从我的 uitabbarcontroller 的 secondviewcontroller (viewcontroller2) 按下按钮,我会将新视图分配给 secondviewcontroller (viewcontroller2) 等等...

    appDelegate.viewController1=[[MyMessages alloc] initWithNibName:@"MyMessages" bundle:nil];
    

    //将其添加到窗口中

    [theWindow addSubview:appDelegate.viewController1.view];
    
    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    

    感谢您的帮助!我已经尝试了一天多来实现它。我希望它能帮助遇到同样问题的人。

    【讨论】:

      【解决方案2】:

      我认为您可能没有正确保留新的视图控制器。 但是你不使用 UINavigtonController 和 pushViewController/popViewController 有什么原因吗?这会让事情变得更容易

      【讨论】:

      • 在下面查看我的答案,我无法将其作为评论发布
      【解决方案3】:

      您可能在发布“新视图”时遇到问题,您指的是视图控制器,而不是控制器的视图。

      我猜你正在尝试这样的事情(或只是举例):

      UIViewController *newVC=[[UIViewController alloc] init...];
      [UIView beginAnimations:@"animId" context:nil];
      [UIVIew transitionFromView:old.view toView:newVC.view duration:0.3f options:nil completion:nil];
      [UIView commitAnimations];
      [newVC release]
      

      您释放了新的视图控制器(插入子视图不会增加retainCount),因此当您执行操作(点击按钮)时,目标(newVC)已被释放,这会产生此错误。

      因此,您所要做的就是创建对此视图控制器的引用(例如,将其分配给某些保留属性)。当您以模态方式执行此操作时,呈现模态的视图控制器会自动将引用保留在 self.modalViewController 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        相关资源
        最近更新 更多