2012-08-17 7 views
7

Tôi cố gắng để sử dụng UIViewController 's transitionFromViewController: toViewController: Phương pháp thời gian nhưng với một hình ảnh động tùy chỉnh.Custom Animation giữa UIViewControllers sử dụng transitionFromViewController: toViewController: thời gian

Tôi có hai bộ điều khiển xem sau thêm vào như trẻ em đến một UIViewController chứa tùy chỉnh:

  1. firstController - Đây là một thể hiện của UITabBarController
  2. secondController - Đây là một lớp con của UIViewController

Mã sau hoạt động như mong đợi:

[self transitionFromViewController:firstController 
        toViewController:secondController 
          duration:2           
          options:UIViewAnimationOptionTransitionFlipFromLeft 
         animations:^(void){} 
         completion:^(BOOL finished){}]; 
.210

Tuy nhiên tôi muốn tạo ra một hình ảnh động tùy chỉnh nơi nơi firstController slide bên trái và được thay thế bằng secondController trượt ở cánh phải tương tự như cách UINavigationControllers đẩy và phương pháp làm việc pop. Sau khi thay đổi options-UIViewAnimationOptionTransitionNone Tôi đã cố gắng để thực hiện hình ảnh động tùy chỉnh trong khối animations nhưng đã hoàn toàn không có thành công. firstController được đổi ngay lập tức cho secondController không có và hoạt ảnh.

Tôi thực sự sẽ đánh giá cao sự giúp đỡ nào.

Cảm ơn bạn

Trả lời

15

Điều này thực sự rất dễ dàng. Vì một lý do nào đó, tôi giả định rằng chế độ xem của secondController sẽ nằm dưới/sau số của firstController. Tôi chỉ cố gắng tạo hiệu ứng cho chế độ xem của firstController. Điều này tất nhiên là sai. Ngay sau khi transitionFromViewController:toViewController:duration được gọi là chế độ xem secondController được đặt trên chế độ xem của firstController. Mã sau đây hoạt động:

CGFloat width = self.view.frame.size.width; 
CGFloat height = self.view.frame.size.height; 

secondController.view.frame = CGRectMake(width, 0, width, height); 

[self transitionFromViewController:firstController 
    toViewController:secondController 
    duration:0.4 
    options:UIViewAnimationOptionTransitionNone 
    animations:^(void) { 
     firstController.view.frame = CGRectMake(0 - width, 0, width, height); 
     secondController.view.frame = CGRectMake(0, 0, width, height); 
    } 
    completion:^(BOOL finished){ 
     [secondController didMoveToParentViewController:self]; 
    } 
]; 
+6

Bạn nên gọi [secondController didMoveToParentViewController: self]; trong trình xử lý hoàn tất quá :) – banDedo

+1

@banDedo Tôi đã cập nhật mã với điều đó. Cảm ơn bạn đã chỉ ra điều đó. Cần thiết đó! –

+0

banDedo & Shaun F. Cảm ơn đề xuất và chỉnh sửa. – Simple99