views:

63

answers:

1

Hello,

I'm using this code after in my ViewDidLoad method :

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];

self.view.frame = CGRectMake(0, 0, 250, 50);

[UIView commitAnimations];

It's working fine but if I try to do the same thing in an other method in the same implementation file like this :

- (void)setViewMovedUp:(BOOL)movedUp {


            NSLog(@"test movedUp ");
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];

            self.view.frame = CGRectMake(0, 0, 250, 50);

            [UIView commitAnimations];



}

Then the animation doesn`t work anymore ( but the NSLog still prints ) . Why would this method behave differently than after "viewDidLoad" ? The setViewMovedUp is called after a button is pressed, therefore I assume that the view is already loaded ? Should I add a condition to make sure that the view is loaded then ?


Edit after Michal's comment :

The button I press uses this IBAction in MyViewController.m :

- (IBAction)viewUp {

    MainViewController *moveUp = [[MainViewController alloc] init];
    [moveUp setViewMovedUp:YES];

}

and this is the code in MainViewController.m :

- (void)setViewMovedUp:(BOOL)movedUp {



    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.view.frame = CGRectMake(0, 0, 250, 250);
    [UIView commitAnimations];



}

The Button's IBAction is not communicating well with the MainViewController method (setViewMovedUp) .

In you example it's working well in the same class .

A: 

You animate different views, self.containerView in first case, self.view in second.

Answer after Julz's edit:

Now it's clear what you're doing wrong. You can't call animations on an object that you just created because it's not on screen yet. You need to execute the animation code later (at least in the next loop's execution), usually with performSelector:withObject:afterDelay or blocks.

For example like this:

- (void)setViewMovedUp:(BOOL)movedUp {
    [self performSelector:@selector(animateViewUp:) withObject:nil afterDelay:0.0];
}

- (void)animateViewUp:(id)o {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.view.frame = CGRectMake(0, 0, 250, 250);
    [UIView commitAnimations];
}
Michal
sorry, that was just a typo, I've edited the question ..
Julz
Are you actually changing the frame ? Because you're setting the same frame in `viewDidLoad` as in `setViewMovedUp:`. Or is it yet another typo ?
Michal
Try to change that NSLog into `NSLog(@"test movedUp from:%@", NSStringFromCGRect(self.view.frame));` and compare the frames. If frames are the same, then there's no animation.
Michal
Yeah, the frames are different before and after. What I don`t get is why the same code is animating the View in the ViewDidLoad and not in the setViewMovedUp...
Julz
I don't understand your part saying "...I assume the view is already loaded". Well, how is your program structured ? Is this code implemented in currently visible view-controller ?
Michal
thanks for the example, it's clear that it's working, I think my problem lies in the way I'm passing the message to move the view up, I`m editing the original question
Julz
Hey Michal, thanks for the explanation, that makes sense . I've implemented the way you did using the 2 different methods, unfortunately it's still not moving ... again, if I take the code from -(void)animateViewUp: and stick it under ViewDidLoad, everything moves up fine ... grrr :)
Julz
When you create `MainViewController` do you setup the frame from where it should be moved ? Also try with longer delay, 0.5f instead of 0.0 ?
Michal
The MainViewController is trying to move a frame that has been setup in IB ( not sure if that is exactly what you mean ) . I've tried with longer delays, but it's the same still not moving . (For information I've got a view halfway at the bottom of the screen and I'm trying to move it up to reveal the rest of the view after pressing a button .)
Julz