瀏覽大圖的一種實現方式,瀏覽大圖實現

來源:互聯網
上載者:User

瀏覽大圖的一種實現方式,瀏覽大圖實現

利用轉場動畫實現(這裡不說轉場動畫),主要就是幾個座標的轉換:將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];}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.