iPhone view move animation

22,581

Solution 1

try this code;

  UIView* view = [self.view viewWithTag:100];
  [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {                 
             CGRect frame = view.frame;
             frame.origin.y = 0;
             frame.origin.x = (-100);
             view.frame = frame;
         }
                         completion:^(BOOL finished)
         {
             NSLog(@"Completed");

         }];

Solution 2

leftFrame is the frame where you can start and right frame is where you want to move to

UIView* view = [self.view viewWithTag:100];
if (!view) {
    NSLog(@"nil");
}
[vw setFrame:leftFrame];
[UIView animateWithDuration:0.3f 
                 animations:^{
                        [vw setFrame:rightFrame];
                 }
                 completion:^(BOOL finished){
                 }
 ];

Solution 3

You can also move your view with smooth animation like this

- (void)moveViewPosition:(CGFloat)xPosition forView:(UIView *)view
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[view setFrame:CGRectMake(xPosition, view.frame.origin.y,view.frame.size.width, view.frame.size.height)];
[UIView commitAnimations];
}

and call it like this

[self moveViewPosition:120.0f forView:yourView];

for more you can also modify it to pass your require duration on function call like this.

- (void)moveViewPosition:(CGFloat)xPosition forView:(UIView *)view withDuration:(float)duration;

there are other options like

 UIView* view = [self.view viewWithTag:99999];
 [UIView animateWithDuration:0.9
                          delay:0.0
                        options: UIViewAnimationCurveEaseOut
                     animations:^
     {                 
         CGRect frame = view.frame;
         frame.origin.y = 68;
         frame.origin.x = 0;
         view.frame = frame;
     }
                     completion:^(BOOL finished)
     {
         NSLog(@"Completed");

     }];
Share:
22,581
will du
Author by

will du

Updated on November 01, 2021

Comments

  • will du
    will du over 2 years

    I want to move one view to right when I clicked a button. I wrote something but it's not working;

    UIView* view = [self.view viewWithTag:100];
    if (!view) {
        NSLog(@"nil");
    }
    
    [UIView animateWithDuration:0.3f 
                     animations:^{
                         [view setTransform:CGAffineTransformMakeTranslation(-100, 0)];
    
                     }
                     completion:^(BOOL finished){
    
                     }
     ];
    
  • TharakaNirmana
    TharakaNirmana almost 9 years
    works for me, for options better to use : UIViewAnimationOptionCurveEaseOut