ios iPhone 视图移动动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14907898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:19:34  来源:igfitidea点击:

iPhone view move animation

iosobjective-ciphoneanimationuiview

提问by will du

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

                 }
 ];

回答by Guru

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

         }];

回答by Lithu T.V

Do it like this .

像这样做 。

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

leftFrame 是您可以开始的帧,而 right 帧是您要移动到的帧

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

快乐编码:)

回答by Usman

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

     }];