2013-02-16 151 views
11

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

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

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

       } 
       completion:^(BOOL finished){ 

       } 
]; 

回答

20

试试这段代码;

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

     }]; 
+0

作品对我来说,为更好地选择使用:UIViewAnimationOptionCurveEaseOut – TharakaNirmana 2015-06-01 09:00:31

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

编码快乐:)

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

,并调用它

对于更多,你也可以修改它来传递你需要的持续时间在这样的函数调用。

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

    }];