2010-01-05 14 views
31

Tôi muốn tạo hiệu ứng chuyển tiếp từ chế độ xem con trở lại chế độ xem siêu.Hoạt hình xóaFromSuperview

tôi hiển thị subview sử dụng:

[UIView beginAnimations:@"curlup" context:nil]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 
[self.view addSubview:self.mysubview.view]; 
[UIView commitAnimations]; 

Các công trình trên tốt. Nó quay trở lại chế độ xem siêu mà tôi không nhận được bất kỳ hoạt ảnh nào:

[UIView beginAnimations:@"curldown" context:nil]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; 
[self.view removeFromSuperview]; 
[UIView commitAnimations]; 

Có điều gì khác tôi nên làm để làm cho chế độ xem phụ hoạt ảnh khi bị xóa không?

Trả lời

23

Tôi nghĩ bạn cần phải làm forView:self.view.superview thay vì để phù hợp với những gì bạn đang làm khi bạn thêm, vì trong trường hợp này, self.view là con và vì vậy bạn sẽ cần thực hiện trên phụ huynh.

+0

nó hoạt động vey tốt ...;) –

97

Nếu bạn đang nhắm mục tiêu iOS 4.0 trở lên, bạn có thể sử dụng khối động thay vì:

[UIView animateWithDuration:0.2 
    animations:^{view.alpha = 0.0;} 
    completion:^(BOOL finished){ [view removeFromSuperview]; }]; 

(mã ở trên xuất phát từ Apple's UIView documentation)

+3

Cảm ơn. Điều đó thật đơn giản. –

+0

than ôi! Tôi luôn nghĩ * xóa * mất 0,2 giây. Nhưng việc loại bỏ xảy ra ngay lập tức. Các thay đổi * alpha *, mất 0,2 giây. Nếu chúng ta đặt nó thành 200 giây một lần nữa thì việc loại bỏ khỏi superview sẽ là tức thời, nhưng nó sẽ không xảy ra cho đến khi 200 giây trôi qua ... – Honey

1

Mặc dù cách tiếp cận với gửi removeFromSuperview nhắn từ khối hoàn thành hình ảnh động hoạt động tốt cho hầu hết các trường hợp, đôi khi không có cách nào để ngăn chế độ xem ngay lập tức bị xóa khỏi hệ thống phân cấp chế độ xem.

Ví dụ: MKMapView xóa các bản xem phụ của nó sau khi nhận được thông báo removeAnnotations và không có thay thế "hoạt hình" cho thông báo này trong API.

Tuy nhiên đoạn mã sau cho phép bạn làm bất cứ điều gì bạn thích với một bản sao hình ảnh của một cái nhìn sau khi nó được lấy ra từ SuperView hoặc thậm chí deallocated:

UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO]; 
snapshotView.frame = view.frame; 
[[view superview] insertSubview:snapshotView aboveSubview:view]; 

// Calling API function that implicitly triggers removeFromSuperview for view 
[mapView removeAnnotation: annotation]; 

// Safely animate snapshotView and release it when animation is finished 
[UIView animateWithDuration:1.0 
        snapshotView.alpha = 0.0; 
       } 
       completion:^(BOOL finished) { 
        [snapshotView removeFromSuperview]; 
       }]; 
9

JosephH câu trả lời trong Swift:

UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0}, 
           completion: {(value: Bool) in 
               view.removeFromSuperview() 
              })