Animation does not seem to work - move view from one position to another on [self dismissViewController

StackOverflow https://stackoverflow.com/questions/22534173

  •  18-06-2023
  •  | 
  •  

Question

I have a view currently on the screen and I would like to move it off the screen during an animation. Its current position is: CGRectMake(0, 168, 320, 50); and I would like to move it to y postion 218.

Here is my code:

 [UIView beginAnimations:Nil context:NULL];
 [UIView setAnimationDuration:0.3];
 self.optionsView.frame = CGRectMake(0, 218, 320, 50);
 [UIView commitAnimations];

Thanks in advance. Nothing is happening, the view isnt moving off screen.

Edit: When the app launches the view is off screen, i then call:

[UIView beginAnimations:Nil context:NULL];
 [UIView setAnimationDuration:0.3];
 self.optionsView.frame = CGRectMake(0, 168, 320, 50);
 [UIView commitAnimations];

to bring the view into the main view. The problem I have is making the view go back off screen again with:

 [UIView beginAnimations:Nil context:NULL];
 [UIView setAnimationDuration:0.3];
 self.optionsView.frame = CGRectMake(0, 218, 320, 50);
 [UIView commitAnimations];

It must be noted that in order to get the view to be off screen on app launch I call:

-(void) viewDidLayoutSubviews{
    self.optionsView.frame = CGRectMake(0, 218, 320, 50);
}

Update: I am also using the camera, i think dismissing the camera is affecting the layouts

Was it helpful?

Solution 2

Use block instead:

[UIView transitionWithView:self.view duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{

    // final view elements attributes goes here

} completion:nil];

This is the modern way of doing animations. You can even specify a completion block, something that should happed after the animation is done, if needed.

OTHER TIPS

I've animated the position of views as follows:

// 'frame' variable previously declared
// 'completion' block previously declared

[UIView animateWithDuration:0.3f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     [_someView setFrame:frame];
                 }
                 completion:^(BOOL finished){
                     if (completion) {
                         completion();
                     }
                 }];

Have you tried it that way?

As Martin Koles has said, the block-based approach to view animation is preferred. The following is in the reference doc for UIView:

Use of the methods in this section is discouraged in iOS 4 and later. Use the block-based animation methods instead.

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html

Try using this

[UIView animateWithDuration:0.3 delay:0 options: UIViewAnimationOptionCurveLinear  
animations:^{
self.optionsView.frame = CGRectMake(0, 218, 320, 50);
} completion:nil];

Worked fine with me. Make sure the view is connected in the xib to its proper object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top