0

I want to do animation like NavigationController pushviewcontroller's animation. but I don't have a NavigationController, I don't want to make it.

So I want to ask is it possible to do it's animation in UIViewController? thanks!


oh forgot to say, I'm trying to switch view after clicking button. Using presentModalViewController now, but I don't like it's animation..

Johnny
  • 2,589
  • 2
  • 27
  • 34

2 Answers2

1

You could animate the origin property of your sub view, make it decreasing along the x axis just after adding it to the main view.

EDIT :

Use something like this :

// retrieve the screen bounds
CGRect sBounds = [[UIScreen mainScreen] bounds];
// the origin point is just on the right of the screen
CGRect newFrame = CGRectMake(sBounds.size.width,
                             0.0,
                             sBounds.size.width,
                             sBounds.size.height);
// set your view frame
[mySecondView setFrame:newFrame];
// add it to the main view
[mainView addSubview:mySecondView];
// then animate your view
[UIView animateWithDuration:0.5     // set the interval you want
                 animations:^{
                     mySecondView.frame.origin = sBounds.origin;
                 }
];
  • You mean animate my second view to the first view? how to do it on the code? – Johnny Jul 11 '11 at 14:12
  • http://stackoverflow.com/questions/5577077/pushviewcontroller-animation-without-actually-calling-pushviewcontroller so I must first add sub view(My second view) and then use [UIView animateWithDuration:1.2 animations:^{ SecondView.view.frame = CGRectMake(320, 0, 320, 480); }]; right? – Johnny Jul 11 '11 at 14:28
  • yes you are right, just in case you don't wanna use the facilities provided by a navigation controller.The thing to understand is that you modify a view property, using the animation facilities of `UIView` –  Jul 11 '11 at 14:42
  • Thanks, I'll try it later. btw [[UIScreen mainScreen] bounds]; what does it mean? – Johnny Jul 11 '11 at 14:47
  • this is the way to retrieve the actual screen bounds (origin position, width, height) –  Jul 11 '11 at 14:49
  • Add this before the line that gives the error : `CGRect vFrame = [[datacontroller view] frame];` and the block body will become : `vFrame.origin = sBounds.origin;` –  Jul 12 '11 at 10:04
  • no memory leak here, because you manipulate only c structs (`CGRect` and `CGFrame`). Removing the first actually depends on your needs, what I do is not removing it and just put it on the right when I don't want it anymore. (the code in the picture is essentially the same as mine, but I just provided the values of the screen bounds rect instead of numbered values). Hope I have helped you as well. –  Jul 12 '11 at 11:42
  • sorry `CGFrame` isn't a type at all, sorry –  Jul 12 '11 at 11:49
  • Thanks for explaining. One more question, sorry, You said that removing view depends on me, If I don't remove the view, just move it out of bound, will it cause program running slow? or not? thank you. – Johnny Jul 12 '11 at 12:04
  • LOL. this thing, I think, depends on the content that your view is providing. Displaying images or video is something more heavy than just displaying few labels, or buttons. –  Jul 12 '11 at 12:15
  • Just a tableview, and few labels. But removeFromSuperview is still the best way right? – Johnny Jul 12 '11 at 12:24
  • that's right, you'd better use `-(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;` in this case so the last argument is actually the block run when the animation completes –  Jul 12 '11 at 12:27
  • I see! The animation does great.But still diffrent with pushviewcontroller. pushviewcontroller really "push" the first view out of bound. And at the same time the second view comes in. Is it possible to do same with it? I tried to use [UIView animateWithDuration:0.5 animations:^{ self.view.frame = CGRectMake(-320,0,320,480;}]; , but.........it will move the second view out of bound too, LOL. I just want to move the main view... – Johnny Jul 12 '11 at 13:02
  • I discovered new problem, a big problem...I have viewWillAppear in Secondview. This void is failed..OMG! – Johnny Jul 12 '11 at 13:22
  • It works fine now if I remove from superview. viewWillAppear works. but Can I set delay to run removeFromSuperview? – Johnny Jul 12 '11 at 13:30
  • 1
    Great, about the delay, learn about `NSTimer` and one of its class method : `-(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats;` –  Jul 12 '11 at 13:32
0

I would use a navigation controller and hide the navigation bar, but as you don't want to do that you can switch between views using [UIView beginAnimantion: @"myAnimation" withContext: nil]; and changing the frame or the origin.

dasdom
  • 13,975
  • 2
  • 47
  • 58