[Request] A good way to use CATransitions

Discussion in 'iOS Development' started by cocotutch, Feb 16, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hiya,

    Since SkylarEC so kindly told me that CATransitions are not impossible and are actually quite easy, I was wondering whether anyone could submit a Tutorial here on a project that;

    Opened up a basic UIWindow.
    Contained a Nav Bar and a Tool Bar.
    Had one Button present which triggered the switch from the current view to the next view, via a "PushFromRight" CATransition.
    And a button present on the other view which switched via a "PushFromLeft" back to where you started again.

    I have this code:

    Code:
    [timeView2 removeFromSuperview];
    				[timeView2 release];
    		
    				
    			
    				[self addSubview:view03];
    			
    				CATransition *animation = [CATransition animation];
    				[animation setType:kCATransitionPush];
    				[animation setSubtype:kCATransitionFromRight];
    		[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
    				[animation setDuration:kAnimationDuration];
    				[[self layer] addAnimation:animation forKey:kAnimationKey];
    Which is doing my head in. It works to get to the next view, but whenever I tap another button, i.e. in the other view, which I have class files for, it crashes due to an "Uncaught Exception".

    My steps:

    Made my view, Identified it as "View02" and created the Source Files.
    Opened up the files and modified them as neccessary with a few UIViewAnimations and stuff to get it right, and then put the above code in to switch from a "Touch Activated" Image View to a "Confirm the close of this view" View.

    The above works.

    I then:

    Went into IB, Identified the "Confirm the close of this view" View, and called it "View03".
    Added an outlet and two actions (the Menu, the view that was switched from, and a Yes and No Button).
    Wired them up after Merging the files.
    Entered the "CATranstion" code to go from the current view back to the menu.

    The above refuses to work. I wired the actions up, but putting [self removeFromSuperView]; doesn't seem to work?

    This may sound really stupid, and n00bish, but could someone write a quick App/Tutorial on how to do this stuff properly?

    cocotutch

    NB: God damn, I hope Apple adds AppleScript to iPhone in the next revision or two, would make creating things a helluva lot easier, then keep Cocoa for the implementations.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Your understanding is fail. You need to look into MVC. Once you understand that, know that you should be using a UINavigationController to push and pop view controllers (of all types). Using a UInavigationController will perform ALL your animations for you "the push animations" you're using).

    And about CATransitions, search. I included two of them in this tutorial: http://www.ifans.com/forums/showthread.php?t=142950

    Download the linked application to see them in action. Although they are not linked to a navigation controller, they work the same way. Just create it and apply it to a layer.
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    To ammend my previous post: don't take that as an insult. My understanding used to also be fail. Back I. The 1.x days, I pushed/popped UINavigationItems manually while animating my view transitions. Which is why a navigation controller does.
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Oh, I see.

    The thing is however, your point IS valid, don't get me wrong...but it seems pointless to me to create a "UINavigationController" based project, if I only need one transition.

    I have a View-based Application now, and have a few animations. I have a button that, on a click, will twist, fade or push the current view to the next one and vice versa whenever I tap the same button on the other view, just with a different BOOL setting.

    At the moment, all my "Confirm action!" views are inactive and UIAlertViews are popping up all over the place, which can lose the novelty after a while.

    Thanks anyway, Ill see if I can get it one day

    Please Register or Log in to view images

  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    The point is not moot. Transitioning between views is the exact reason the UINavigationController was created. If you don't need a UINavigationBar, hide it. And you can do other transitions with a UINavigationController than jsut push.

    Also, Apple is very strict about using MVC, so get used to it.

Share This Page