瀏覽大圖的一種實現方式,瀏覽大圖實現
利用轉場動畫實現(這裡不說轉場動畫),主要就是幾個座標的轉換:將cell上的imageView快照產生一個snapView(直接建立一個ImageVIew也一樣), 在將cell上image的frame 座標轉換到containerView上,在將snapView放大到目尺規寸 (首先你要知道轉場動畫時怎麼一回事)。
下面是主要代碼,一個自訂類繼承自NSObject。實現了UINavigationControllerDelegate、UIViewControllerAnimatedTransitioning協議
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { // 擷取當前的控制器 UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; // 擷取將要轉向的控制器 UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // 擷取[內容] 檢視 UIView *containerView = [transitionContext containerView]; UICollectionView *collectionView ; UIImageView *contentImageView; if (self.opration == PushAnimationOprationPush) { ViewController *from = (ViewController *)fromVC; collectionView = from.collectionView; contentImageView = [toVC valueForKey:@"_contentImageView"]; }else { ViewController *to = (ViewController *)toVC; collectionView = to.collectionView; contentImageView = [fromVC valueForKey:@"_contentImageView"]; } // 擷取選中的cell 的indexpath NSArray *selectedItems = [collectionView indexPathsForSelectedItems]; // 根據indexpath 擷取 cell CusCollectionViewCell *cell = (CusCollectionViewCell *)[collectionView cellForItemAtIndexPath:[selectedItems firstObject]]; UIView *snapView; CGRect snapViewFrame; CGRect snapViewTargetFrame; if (self.opration == PushAnimationOprationPush) { // 截取cell 上contentImage 的快照 snapView = [cell.contentImage snapshotViewAfterScreenUpdates:NO]; // 根據contentImage在cell上的座標 轉換到 containerView上 snapViewFrame = [containerView convertRect:cell.contentImage.frame fromView:cell.contentImage.superview]; // 擷取目標控制器上要顯示的 imageView,並根據該imageView的尺寸位置,轉換到 [內容] 檢視上,成為snapView 最終尺寸。 snapViewTargetFrame = [containerView convertRect:contentImageView.frame fromView:contentImageView.superview]; }else { snapView = [contentImageView snapshotViewAfterScreenUpdates:NO]; snapViewFrame = [containerView convertRect:contentImageView.frame fromView:contentImageView.superview]; snapViewTargetFrame = [containerView convertRect:cell.contentImage.frame fromView:cell.contentImage]; } snapView.frame = snapViewFrame; toVC.view.alpha = 0; [containerView addSubview:toVC.view]; [containerView addSubview:snapView]; contentImageView.hidden = YES; cell.contentImage.hidden = YES; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.6f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ snapView.frame = snapViewTargetFrame; toVC.view.alpha = 1.0; } completion:^(BOOL finished) { contentImageView.hidden = NO; cell.contentImage.hidden = NO; [snapView removeFromSuperview]; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { if (operation == UINavigationControllerOperationPush) { self.opration = PushAnimationOprationPush; }else if (operation == UINavigationControllerOperationPop) { self.opration = PushAnimationOprationPop; } return self;}- (void)start { self.nav.delegate = self;}/** 為目標控制器添加一個tap手勢,返回上一個控制器 */- (void)tapGestureToPopWithController:(UIViewController *)targetVC { self.targetViewController = targetVC; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)]; [targetVC.view addGestureRecognizer:tapGesture];}- (void)tapGesture:(UITapGestureRecognizer *)gesture { [self.targetViewController.navigationController popViewControllerAnimated:YES];}