2012-05-28 38 views
10

Tôi đang cố gắng sử dụng phân đoạn tùy chỉnh để thực hiện một loại hoạt ảnh thu phóng. Khi quá trình chuyển đổi được thực thi, sourceViewController chuyển sang màu đen, khi đó zoom sẽ xuất hiện.Hoạt ảnh phân đoạn tùy chỉnh

Cố gắng cũng đặt pushViewController: thành khối hoàn thành nhưng quá trình chuyển đổi không được thực thi.

- (void)perform { 

    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController; 
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController; 

    [destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
    [destinationViewController.view setAlpha:0.0]; 

    [UIView animateWithDuration:0.5 
          delay:0.0 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
         [destinationViewController.view setAlpha:1.0]; 
         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO]; 
        } 
        completion:^(BOOL finished){ 
        }]; 

} 

Tôi đang làm gì sai?

Trả lời

16

Nó cảm thấy kludgy, nhưng bạn có thể thử thêm destinationViewController.view dưới dạng phần xem trước trước khi hoạt ảnh của bạn và sau đó khi hoạt ảnh được thực hiện, xóa nó và đẩy nó trở lại mà không có hoạt ảnh. Giải quyết các màn hình màu đen trước khi chuyển đổi, nhưng có lẽ không hoàn hảo, phụ thuộc vào những gì bạn muốn làm với thanh điều hướng, nhưng có lẽ gần gũi hơn:

[sourceViewController.view addSubview:destinationViewController.view]; 
[destinationViewController.view setFrame:sourceViewController.view.window.frame]; 
[destinationViewController.view setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
[destinationViewController.view setAlpha:1.0]; 

[UIView animateWithDuration:0.5 
         delay:0.0 
        options:UIViewAnimationCurveEaseOut 
       animations:^{ 
        [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
        [destinationViewController.view setAlpha:1.0]; 
       } 
       completion:^(BOOL finished){ 
        [destinationViewController.view removeFromSuperview]; 
        [sourceViewController.navigationController pushViewController:destinationViewController animated:NO]; 
       }]; 

Note, iOS hiệu quả 7, bạn sẽ sử dụng hiệu ứng chuyển tiếp tùy chỉnh . Để biết thêm thông tin, xem WWDC 2013 Custom Transitions Using View Controllers.

Ví dụ, nếu cố gắng làm một sự chuyển tiếp tùy chỉnh với bộ điều khiển chuyển hướng, bộ điều khiển xem đầu tiên sẽ xác định chính nó như là các đại biểu của bộ điều khiển chuyển hướng:

self.navigationController.delegate = self; 

Sau đó, nó sẽ xác định làm phim hoạt hình tùy chỉnh cho push và pop, tương ứng:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController 
            animationControllerForOperation:(UINavigationControllerOperation)operation 
               fromViewController:(UIViewController *)fromVC 
               toViewController:(UIViewController *)toVC 
{ 
    if (operation == UINavigationControllerOperationPush) { 
     return [[PushAnimator alloc] init]; 
    } else { 
     return [[PopAnimator alloc] init]; 
    } 
} 

và sau đó bạn muốn rõ ràng là thực hiện các họa sĩ:

@interface PushAnimator : NSObject <UIViewControllerAnimatedTransitioning> 

@end 

@implementation PushAnimator 

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    return 0.5; 
} 

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 

    [[transitionContext containerView] addSubview:toViewController.view]; 
    toViewController.view.frame = fromViewController.view.frame; 
    toViewController.view.transform = CGAffineTransformMakeScale(0.5,0.5); 
    toViewController.view.alpha = 0.0; 

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ 
     toViewController.view.transform = CGAffineTransformIdentity; 
     toViewController.view.alpha = 1.0; 
    } completion:^(BOOL finished) { 
     [fromViewController.view removeFromSuperview]; 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

@end 

@interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning> 

@end 

@implementation PopAnimator 

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{ 
    return 0.5; 
} 

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 

    [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view]; 
    toViewController.view.frame = fromViewController.view.frame; 

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:0 animations:^{ 
     fromViewController.view.transform = CGAffineTransformMakeScale(0.5,0.5); 
     fromViewController.view.alpha = 0.0; 
    } completion:^(BOOL finished) { 
     [fromViewController.view removeFromSuperview]; 
     [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; 
    }]; 
} 

@end 
+0

cảm ơn, nó hoạt động tốt! – Nimrod7

Các vấn đề liên quan