iPhone视图移动动画

11

当我点击按钮时,我希望将一个视图向右移动。我已经写了一些代码,但它没有生效。

UIView* view = [self.view viewWithTag:100];
if (!view) {
    NSLog(@"nil");
}

[UIView animateWithDuration:0.3f 
                 animations:^{
                     [view setTransform:CGAffineTransformMakeTranslation(-100, 0)];

                 }
                 completion:^(BOOL finished){

                 }
 ];
3个回答

21

尝试使用这段代码:

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

         }];

对我来说没问题,对于选项最好使用:UIViewAnimationOptionCurveEaseOut - TharakaNirmana

2

左侧框架是您可以开始的框架,右侧框架是您要移动到的框架。

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

2
您还可以像这样平滑地移动视图:

- (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];
}

然后像这样调用它

[self moveViewPosition:120.0f forView:yourView];

如果需要,您还可以通过以下方式在函数调用中传递所需的持续时间来进行修改。

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

还有其他选项,比如

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

     }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接