1

Is it possible (at all) to load a XIB with custom AnimationTransition?

What I have done, is creating an animation that "Covers" the screen, and what I want is that after that animation has done playing, I would like it to display the new XIB.

I cant seem to find any proper solution to this...Any ideas?

To be more clear: Press a button --> Play Animation (cover screen) --> Load XIB.


Hello again! Yes, the last way you described is the way I am doing it. I have two UIViews (Might be wrong already there), that are placed off-bounds on each side, (like x.-160.y.0 and x.320y.0)

-(IBAction) leftDoor{

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    closeDoor1.center = CGPointMake( closeDoor1.center.x +160, closeDoor1.center.y);


    [UIView commitAnimations];

 } 


-(IBAction) rightDoor{

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    closeDoor2.center = CGPointMake( closeDoor2.center.x -160, closeDoor2.center.y);

    [UIView commitAnimations];
}

So, what I am going to do is not to "split" the current view and then open a new XIB, the effect I am searching for is a "closing door" effect, thats why I used UIView ( thought I place graphics on top of those, like two ImageViews). Then, for loading the new XIB...This is where im really puzzled. My first way of trying this out was to make three IBActions, including the two I mentioned above, and then apply all three (multiple actions) to a button. So for switching views I did something like this: `-(IBAction) newViewDisplay:(id)sender{

theView *newViewController = [[theView alloc]
                                      initWithNibName:@"theView" bundle:nil]; 

[self.view addSubview:newViewController.view];

} `

As you said, this might be over my head, but if I just got some directions, I´ll walk miles to make this work. It would really give my app a facelift. A huge thanks for taking time to answer my question, All the best/Andy

Sampson
  • 265,109
  • 74
  • 539
  • 565
andy
  • 17
  • 4
  • What's in the XIB? Is it a view controller or just a view, etc.? Then you need to load it. After that, you can apply the animation. – W Dyson May 29 '11 at 19:19
  • Its a ViewController. So, If I load it first, do I declare the animation afterwards in the code? Within the function? Sry, bit confused... Thanks! – andy May 29 '11 at 20:25
  • Ya, you load it, add an animation block, then animate the new view onto the screen. – W Dyson May 29 '11 at 21:41
  • Thank you for your answer! I tried it, but it did not seem to work...I dont know if I was clear enough, but I want the "animation" (covers the screen from off-bounds UIViews, the meet in the middle) to start and end before the new XIB is displayed. I dont know if this even is possible, but Ive seen it in some apps...If you had the time, could you type some code and point me in the right direction? If you do not have the time, Still big thanks for your answer. If you post it as an answer I could accept it. Thanks – andy May 29 '11 at 22:22

1 Answers1

2

What are you covering the screen with?

Think of it this way, (it sounds like) you have 2 views, the old one and the new one that is stored in this xib. The animation is secondary.

You need to load the new view, then display it (can be off-screen), then animate it (move it) into the place you want it to go. If you want to split it into two parts, one at the bottom and one at the top of the screen, then meet in the middle, I think that's both complicated and above you skill level. (Not trying to be mean here).

If you are trying to do a split animation as described, it can be done, but you need to 'fake it'. It would involve taking a 'screenshot' (of sorts), splitting it, moving these two images so they meet with an animation, loading the view underneath, and then removing the images. Tricky stuff.

Do you have to have this sort of animation?

If you could post the code you have, I can rearrange it, and add to it, for you.

You'll need to clarify what it is exactly you want to do, though.

UPDATE 3: I added another option for autoreverse.

//You can add both doors into one animation block.

    //Create an animation block. Ease Out is better for this animation but you can change it.
[UIView animateWithDuration:1.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse) animations:^{
    closeDoor1.center = CGPointMake( closeDoor1.center.x +160, closeDoor1.center.y);
    closeDoor2.center = CGPointMake( closeDoor2.center.x -160, closeDoor2.center.y);

}completion:^(BOOL finished){
    if (finished) {
            //When finished, create the VC if it doesn't exist, and insert it below the 'closed door'.

        if (newViewController == nil) {
            UIViewController *newViewController = [[UIViewController alloc] initWithNibName:@"theView" bundle:nil]; 
        }

        [self.view insertSubview:newViewController.view belowSubview: closeDoor2];;
        [self.closeDoor1 removeFromSuperview];
        [self.closeDoor2 removeFromSuperview];

    }
}];
W Dyson
  • 4,604
  • 4
  • 40
  • 68
  • Im new to stackoverflow, so below is my answer to the comment :) Some code, and some explanation. Thanks! – andy May 30 '11 at 09:59
  • Just edit your origional question, and move what you have in your answer to your origional question. – W Dyson May 30 '11 at 12:17
  • Thank you soooo much! The solution to this might be just in front of my me, but...I implemented your code (again, THANKS) and also created a new VC in the XIB (the one where the button is) connected the "newViewController" to Files Owner, and the animation worked fine..Although, when finished, it only displays a white screen. Im sure I must have missed a step. What do you think might be the problem? – andy May 30 '11 at 14:29
  • No problem, I forget to remove the 'doors'. The example is updated. Just play around with things, but this animation block is what you want, I think. Remember though, blocks are new as of iOS 4.0, they won't work before that. If you need to support 3.2 and below, we can do that, but it's more work. – W Dyson May 30 '11 at 14:56
  • My friend, YOU ARE A-W-E-S-O-M-E! It worked like a charm, and seriously, I made a victory dance when I got this to work! What I am going to do now (to complete it) is to make a animation "reverse" in viewDidLoad, so when loading the new view, the user will feel like a shutter has been opened and closed. Again, THANK you my friend, All my best and have a great day! – andy May 30 '11 at 15:49
  • Hi again.. Sorry to ask this but..I cant seem to get two things to work. First, When the new view is loaded, I would like to have the same animation, but reversed, so that it "opens up" to the new view, as of now, it only pops up when the animation is finished. Then, I wonder if you know how to implement the same animation when pressing a the "back" button? Sorry to be asking for help again, I stayed up all night trying figure this out for myself. Anyway, If you do have time to point me in the right direction (no need to write the full code, just some help) I would be really thankful. Thanks! – andy May 31 '11 at 07:41
  • I forgot that you wanted autoreverse, I updated the code. As for a 'back'button, how about making the code I posted above into its own method so you can call it arbitrarily. – W Dyson May 31 '11 at 12:40
  • Thanks again. Youre far too kind for helping me this much. I tried the autoreverse, and it worked fine :) Although it does not show the new view until the animation has ended, which gives a slight delay effect. But I will try to fix this :) Again, thank you so much. – andy May 31 '11 at 13:11
  • So, I managed to get the animation to work just fine, and instead of autoreverse, I made a animation that shows up in the new XIB (same animation but from middle)..It looks really cool. Although...there is one thing I really cant get to work, and it is the return method. The button to the new view gets disabled and stops working...How should it really be done (in code) in order to "get back and forth"? Meaning from the newView back to the old view. I have tried removeFromSuperview, and I do get "back", but I cant seem to access that XIB twice. Thanks! – andy Jun 02 '11 at 12:45
  • You need to use a delegate method to call dismiss in the parent view (either in the original view or the app delegate if that's how you have it set up). Use a modal delegate as a template. – W Dyson Jun 02 '11 at 12:59
  • Alright...I think I know what to do. Not 100% sure, but I´ll try googling some otherwise. Thanks again! – andy Jun 02 '11 at 13:41
  • Did not seem to find the answer I was looking for.. Im sorry for taking up so much of your time, but If you do have the time to just explain a little more, that would be great. I thought you ment like this: I should have a method in my "mainView" that implements a delegate method? Again sorry for beign dumb. This is all new to me and I couldnt find any documentation in order to understand. Thanks again my friend. – andy Jun 02 '11 at 21:49
  • Search for obj-c delegate topics, and modal view dismissal. The flip template in XCode has this. You need to call the delegate in the door view and call a method in the other view to dismiss it. – W Dyson Jun 02 '11 at 22:48