简体   繁体   English

使用CATransition从右向左和向后过渡?

[英]Transition right to left and back using CATransition?

I have an uitabbarcontroller which consists of five uiviewcontrollers. 我有一个由五个uiviewcontrollers组成的uitabbarcontroller。

When i tap a button in the view of the first uiviewcontroller i want the uiviewcontroller to hide and a new uiviewcontroller to be added with a transition from right to left. 当我在第一个uiviewcontroller的视图中点击一个按钮时,我希望uiviewcontroller隐藏,并添加一个新的uiviewcontroller,并从右向左过渡。 I have achieved this but when i tap on a button on the new view the app crashes!!! 我已经做到了,但是当我点击新视图上的按钮时,应用程序崩溃了!!! There is no code attached to the button. 按钮上没有附加代码。 I get EXEC_BAD_ACCESS error. 我收到EXEC_BAD_ACCESS错误。

Does anyone know why is this happening and how to overcome this problem with the new view? 有谁知道这是为什么发生的,以及如何用新的观点克服这个问题?

When i try a modalview transition the view functions properly. 当我尝试模式视图过渡时,视图功能正常。 This happens when i try to use CATransition. 当我尝试使用CATransition时会发生这种情况。 I need CATransition because modalview transitions do not have a transition from right to left or left to right! 我需要CATransition,因为modalview过渡没有从右到左或从左到右的过渡!

Any help appreciated! 任何帮助表示赞赏!

This is the code that needed to make the transition between an Uiview of a tabviewcontroller and a new uiview. 这是在tabviewcontroller的Uiview和新uiview之间进行转换所需的代码。 The code goes to a button's IBAction which exists in one of the uiviews of the tabviewcontroller. 该代码转到位于tabviewcontroller的uiview之一中的按钮的IBAction。

//initialize the app delegate //初始化应用程序委托

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

// get the view that's currently showing //获取当前显示的视图

UIView *currentView = self.view; UIView * currentView = self.view;

// get the the underlying UIWindow, or the view containing the current view view //获取基础的UIWindow或包含当前视图的视图

UIView *theWindow = [currentView superview]; UIView * theWindow = [currentView superview];

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

//assign the new view to the viewcontroller1. //将新视图分配给viewcontroller1。 It is the first uiviewcontroller of the uitabbarcontroller. 它是uitabbarcontroller的第一个uiviewcontroller。 My uitabbarcontroller consists of five uiviewcontrollers in my project. 我的uitabbarcontroller由我的项目中的五个uiviewcontrollers组成。 The button exists in the first viewcontroller1 this is why i am assigning the new view to the viewcontroller1. 该按钮存在于第一个viewcontroller1中,这就是为什么我要将新视图分配给viewcontroller1的原因。 If the button was pressed from the secondviewcontroller (viewcontroller2) of my uitabbarcontroller i will assign the new view to the secondviewcontroller (viewcontroller2) and so on... 如果从我的uitabbarcontroller的secondviewcontroller(viewcontroller2)按下按钮,我会将新视图分配给secondviewcontroller(viewcontroller2),依此类推...

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

//add it to the window //将其添加到窗口

[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"];

Thanks for you help! 感谢您的帮助! I have been trying this for more than a day to achieve it. 我已经尝试了一个多天以实现它。 I hope it will help someone with the same problem. 我希望它将对遇到相同问题的人有所帮助。

I think that you're probably not retaining the new view controller properly. 我认为您可能没有正确保留新的视图控制器。 But is there a reason you don't use an UINavigatonController and pushViewController/popViewController ? 但是有没有理由不使用UINavigatonController和pushViewController / popViewController吗? That would make things much easier 那会让事情变得容易得多

You probably have problem with releasing the "new view", which by that You mean the view controller, not controler's view. 您可能在发布“新视图”时遇到问题,这意味着您是视图控制器,而不是控制者的视图。

I guess You're trying something like this(or just for example): 我猜您正在尝试类似的操作(或仅作为示例):

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]

You release the new view controller (inserting subview doesn't increment the retainCount), so when You perform action (tap button), the target(newVC) is already released and this creates this error. 您释放了新的视图控制器(插入子视图不会增加keepCount),因此,当您执行操作(点击按钮)时,目标(newVC)已被释放,并且会产生此错误。

So, all You have to do is create reference to this view controller(fe assign it to some retained property). 因此,您要做的就是创建对此视图控制器的引用(将其分配给某些保留的属性)。 When You do this modally, view controller presenting the modal automatically keeps the reference in self.modalViewController. 当您以模态方式执行此操作时,呈现模态的视图控制器会自动将引用保留在self.modalViewController中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM