iOS:使用动画子视图在视图之间转换

时间:2014-03-20 08:07:19

标签: ios objective-c autolayout

什么是"权利"在转换期间移动视图时在视图之间转换的方法?

基本上,我想要一些像打开Photos.app并点击照片的东西:当缩略图从应用程序从集合视图转换到详细视图时,它会扩展为全屏。

最好使用自动布局来完成,但我不知道如何...

谢谢!

1 个答案:

答案 0 :(得分:0)

,我认为不能使用自动布局来完成。您可以使用视图控制器来显示全屏图像,让我们说PhotoViewController。现在,您可以使用 presentViewController 以模式方式显示PhotoViewController,使用以下代码:

<强> Note: Code Mentioned below assumes you are using iOS 7 or later sdk.

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 PhotoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PhotosView"];
 vc.view.backgroundColor = [UIColor clearColor];
 vc.transitioningDelegate = self;
 vc.modalPresentationStyle = UIModalPresentationCustom;
 [self presentViewController:vc animated:YES completion:nil];

现在,在animateTransition方法中,您可以使用以下内容:

 -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    NSIndexPath *selected = self.collectionView.indexPathsForSelectedItems[0];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:selected];

    UIView *container = transitionContext.containerView;

    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;

    CGRect beginFrame = [container convertRect:cell.bounds fromView:cell];

    // Would be safer to use container bounds here
    CGRect endFrame = [transitionContext initialFrameForViewController:fromVC];

    // NOTE: Remove this line for full screen goodness
    endFrame = CGRectInset(endFrame, 60.0, 60.0);

    if (toVC.isBeingPresented) {
        toView.frame = beginFrame;
        [container addSubview:toView];
        cell.hidden = YES;
    }
    [UIView animateWithDuration:1.0 delay:0
         usingSpringWithDamping:500 initialSpringVelocity:15
                        options:0 animations:^{
                            if (toVC.isBeingPresented) {
                                toView.frame = endFrame;
                            } else {
                                fromView.frame = beginFrame;
                                cell.hidden = NO;
                                [fromView removeFromSuperview];
                            }
                        }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition: YES];
                     }];
    }

希望这有帮助,如果您有疑问,请随时回复。