iPhone view move animation

2019-02-12 15:47发布

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){

                 }
 ];

3条回答
一纸荒年 Trace。
2楼-- · 2019-02-12 15:56

Do it like this .

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){
                 }
 ];

Happy Coding :)

查看更多
地球回转人心会变
3楼-- · 2019-02-12 16:07

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");

         }];
查看更多
来,给爷笑一个
4楼-- · 2019-02-12 16:16

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");

     }];
查看更多
登录 后发表回答