2013-02-16 41 views
11

Tôi muốn di chuyển một chế độ xem sang phải khi tôi nhấp vào một nút. Tôi đã viết một cái gì đó nhưng nó không hoạt động;Xem hoạt ảnh trên iPhone di động

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

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

       } 
       completion:^(BOOL finished){ 

       } 
]; 

Trả lời

20

thử mã này;

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

công trình đối với tôi, cho các tùy chọn tốt hơn để sử dụng: UIViewAnimationOptionCurveEaseOut – TharakaNirmana

2

Làm như thế này .

leftFrame là khung nơi bạn thể bắt đầu và khung bên phải là nơi bạn muốn di chuyển đến

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

Chúc mừng Mã hóa :)

2

Bạn còn có thể chuyển cái nhìn của bạn với hình ảnh động trơn tru như

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

và gọi nó như thế này

để biết thêm bạn cũng có thể sửa đổi nó để vượt qua thời gian yêu cầu của bạn trên cuộc gọi chức năng như thế này.

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

có những lựa chọn khác như

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

    }]; 
Các vấn đề liên quan