2013-02-16 17 views
11

Chcę przenieść jeden widok w prawo po kliknięciu przycisku. Napisałem coś, ale to nie działa;Animacja przeniesienia widoku iPhone'a

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

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

       } 
       completion:^(BOOL finished){ 

       } 
]; 

Odpowiedz

20

wypróbuj ten kod;

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

prace dla mnie, dla opcji lepiej jest użyć: UIViewAnimationOptionCurveEaseOut – TharakaNirmana

2

Zrób to w ten sposób .

leftFrame jest ramka, w którym można rozpocząć i prawa ramka jest tam, gdzie chcesz, aby przejść do

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

Szczęśliwy Coding :)

2

Można również zmieniać widok z płynną animacją jak ten

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

i nazwać tak

Aby uzyskać więcej, można również zmodyfikować go, aby spełnić swój wymagany czas trwania w takim wywołaniu funkcji.

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

istnieją inne opcje, takie jak

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

    }];