An Implementation Method for browsing a large image.
The transfer animation is used (not to mention the transfer animation here). It mainly refers to the conversion of several coordinates: The imageView snapshot on the cell is generated to generate a snapView (the same is true for directly creating an ImageVIew ), after converting the frame coordinate of the image on the cell to containerView, zoom in to the target size (First you need to know what is going on in the Transition animation ).
The following is the main code. A custom class inherits from NSObject. The UINavigationControllerDelegate and UIViewControllerAnimatedTransitioning protocols are implemented.
-(Void) animateTransition :( id <UIViewControllerContextTransitioning>) transitionContext {// obtain the current controller UIViewController * fromVC = [transitionContext viewControllerForKey: Signature]; // obtain the Controller UIViewController * toVC = [transitionContext viewControllerForKey: UITransitionContextToViewControllerKey]; // obtain the container view UIView * containerView = [transitionContext containerView]; UI CollectionView * collectionView; UIImageView * contentImageView; if (self. opration = PushAnimationOprationPush) {ViewController * from = (ViewController *) fromVC; collectionView = from. collectionView; contentImageView = [toVC valueForKey: @ "_ contentImageView"];} else {ViewController * to = (ViewController *) toVC; collectionView =. collectionView; contentImageView = [fromVC valueForKey: @ "_ contentI MageView "];} // obtain the indexpath NSArray * selectedItems = [collectionView metrics] of the selected cell; // obtain the cell CusCollectionViewCell * cell = (CusCollectionViewCell *) [collectionView cellForItemAtIndexPath: [selectedItems firstObject]; UIView * snapView; CGRect snapViewFrame; CGRect snapViewTargetFrame; if (self. opration = PushAnimationOprationPush) {// capture the contentImage snapshot on the cell SnapView = [cell. contentImage snapshotViewAfterScreenUpdates: NO]; // convert the coordinates of the contentImage to the snapinerview snapViewFrame = [containerView convertRect: cell. contentImage. frame fromView: cell. contentImage. superview]; // obtain the imageView to be displayed on the target controller, and convert it to the container view based on the size and position of the imageView to become the final size of the 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. 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: Refreshing animations: ^ {snapView. frame = snapView TargetFrame; toVC. view. alpha = 1.0;} completion: ^ (BOOL finished) {contentImageView. hidden = NO; cell. contentImage. hidden = NO; [snapView removeFromSuperview]; [transitionContext completeTransition :! [TransitionContext transitionWasCancelled];}
-(Id <strong>) navigationController :( UINavigationController *) navigationController animationControllerForOperation :( strong) operation fromViewController :( UIViewController *) fromVC toViewController :( UIViewController *) toVC {if (operation = running) {self. opration = PushAnimationOprationPush;} else if (operation = UINavigationControllerOperationPop) {self. opration = PushAnimationOprationPop;} return self;}-(void) start {self. nav. delegate = self;}/** adds a tap gesture to the target controller and returns the last controller */-(void) tapGestureToPopWithController :( UIViewController *) targetVC {self.tar getViewController = targetVC; UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapGesture :)]; [targetVC. view addGestureRecognizer: tapGesture];}-(void) tapGesture :( UITapGestureRecognizer *) gesture {[self.tar getViewController. navigationController popViewControllerAnimated: YES];}