11

单击按钮时,我想将一个视图向右移动。我写了一些东西,但它不起作用;

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

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

                 }
                 completion:^(BOOL finished){

                 }
 ];
4

3 回答 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");

         }];
于 2013-02-16T07:10:35.687 回答
2

leftFrame 是您可以开始的框架,而右框架是您要移动到的地方

UIView* view = [self.view viewWithTag:100];
if (!view) {
    NSLog(@"nil");
}
[vw setFrame:leftFrame];
[UIView animateWithDuration:0.3f 
                 animations:^{
                        [vw setFrame:rightFrame];
                 }
                 completion:^(BOOL finished){
                 }
 ];
于 2013-02-16T07:10:55.897 回答
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");

     }];
于 2015-07-16T15:43:34.717 回答