views:

37

answers:

2

Hello ,

I'm using this method to try to move my view up but without success, this is the code in my MainViewController implementation file :

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

CGRect rect = self.view.frame;
if (movedUp)
{
    NSLog(@"should move Up");
    rect.origin.y -= 50;
    rect.size.height += 300;
}
self.view.frame = rect ;

[UIView commitAnimations];

It is not erroring out and it's printing out "should move Up" so I know that the method is executed properly .

This is the IB hierarchy ( I`m trying to move the highlighted View) :

alt text

Any help would be greatly appreciated, I`m running out of ideas ..


Ok, Editing here I've isolated the problem to be something else, I'm using this code now :

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

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

[UIView commitAnimations];

and if I injecting this code in "- (void)viewDidLoad" it's working fine .

but if I use the same code in a different function in the same ViewController implementation file , it's not working :

- (void)setViewMovedUp:(BOOL)movedUp {


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

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

        [UIView commitAnimations];



}

BUT it's still printing out "test moved up" so the code is executed, just the view not animating .

A: 

use [self.view setFrame:rect];

istant of

self.view.frame = rect ;

(i Think Animation will not uses for Self.view)

Ashish Mathur

Ashish Mathur
Hmm, unfortunately it's still not moving up, still printing out the "should move up " message and not errors/warnings :(
Julz
+1  A: 

Try moving another view - not the main one.
Maybe there is a problem moving the main view.

If it works then add one more view to your views hierarchy that will contain all the others and connect it to a IBOutlet.

If it doesn't work then comment me and we'll try to figure it out...

Michael Kessler
ah, that's interesting . How would I explicitly specify an other view then ?
Julz
What do you mean? You can create add `IBOutlet UIView *containerView;` line in your view controller's .h file and connect it to some view in your views hierarchy (for example the one that is right under the main view).
Michael Kessler
Hey Michael, I did that : created a new containerView Outlet and linked it in IB, changed the code to self.containerViewframe instead of self.view.frame but unfortunately the view still doesn't move, ( no errors or warnings though ) .
Julz
Maybe this code is executed from non-main thread? Can you explain how and when this code is executed? Also try getting rid of the animation - just set the frame and see if it still doesn't work.
Michael Kessler
Well, the funny thing is if I stick my animation code under "ViewDidLoad" it's working fine, as soon as I stick the same code under my own method (setViewMovedUp) it just prints the Log but does not move the view like it does with "ViewDidLoad"
Julz
I saw this comment to another answer. This is why I asked you about the thread. `viewDidLoad` is executed from the main thread and it has all the rights to manipulate view. There is a chance that your method is called from non-main thread and this is why you can't animate the views... Try to call the `setViewMovedUp` like this: `[self performSelectorOnMainThread:@selector(setViewMovedUp) withObject:nil waitUntilDone:NO]];` (instead of `[self setViewMovedUp];`)
Michael Kessler