印象筆記轉場和彈簧特效的實現

寫這個demo緣于一次公司內部技術的分享,一直以來在閑余時間都會去研究各個精選app的一些內部交互方式,順便也會去猜測每個產品設計種種很炫的交互的初衷,今天就拿印象筆記的的轉場和彈簧效果就此剖析一下,期間我也在網上找了一些該方面的資料和類似的demo,經過一天的整合,寫了如下和大伙共同交流的文檔,有好的意見和建議隨時@me,謝謝!

當collection view的布局改變時,我們自定義的布局必須被丟棄,但這滾動并不會影響到布局。幸運的是,collection view將它的新bounds傳給shouldInvalidateLayoutForBoundsChange: method。這樣我們便能比較視圖當前的bounds和新的bounds:

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

Simulator Screen Shot 2015年11月13日 上午1.05.21.png

這個動畫只在collectionview滑動到頂部和底部會觸發,重寫layoutAttributesForElementsInRect這個方法根據collectionview的contentoffset計算出cell的frame,這樣就ok了。

    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let offsetY = self.collectionView!.contentOffset.y
        let attrsArray = super.layoutAttributesForElementsInRect(rect)
        let collectionViewFrameHeight = self.collectionView!.frame.size.height;
        let collectionViewContentHeight = self.collectionView!.contentSize.height;
        let ScrollViewContentInsetBottom = self.collectionView!.contentInset.bottom;
        let bottomOffset = offsetY + collectionViewFrameHeight - collectionViewContentHeight - ScrollViewContentInsetBottom
        let numOfItems = self.collectionView!.numberOfSections()
      
        for attr:UICollectionViewLayoutAttributes in attrsArray! {
            if (attr.representedElementCategory == UICollectionElementCategory.Cell) {
                var cellRect = attr.frame;
                if offsetY <= 0 {
                    let distance = fabs(offsetY) / SpringFactor;
                    cellRect.origin.y += offsetY + distance * CGFloat(attr.indexPath.section + 1);
                }else if bottomOffset > 0 {
                    let distance = bottomOffset / SpringFactor;
                    cellRect.origin.y += bottomOffset - distance *
                        CGFloat(numOfItems - attr.indexPath.section)
                }
                attr.frame = cellRect;
            }
        }
        return attrsArray;
    }

轉場效果:

Simulator Screen Shot 2015年11月13日 上午1.05.41.png

自定義類EvernoteTransition遵守UIViewControllerAnimated
Transitioning和UIViewControllerTransitioningDelegate,
該類的對象作為transitioningDelegate。實現
UIViewControllerAnimatedTransitioning中
的transitionDuration和animateTransition方法前者返回動畫的時間,后者用來實現轉場時的具體動畫。
在UIViewControllerTransitioningDelegate的present和dismiss代理方法中返回EvernoteTransition對象,這樣就ok了

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

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *nextVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [transitionContext containerView].backgroundColor = kBGColor;
    _selectClell.frame = _isPresent ? _originFrame : _finalFrame;
    UIView *addView = nextVC.view;
    addView.hidden = _isPresent ? YES : NO;
    [[transitionContext containerView] addSubview:addView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        for (CollectionViewCell *visibleCell in self.visibleCells) {
            if (visibleCell != self.selectClell) {
                CGRect frame = visibleCell.frame;
                if (visibleCell.tag < self.selectClell.tag) {
                    CGFloat yDistance = self.originFrame.origin.y - self.finalFrame.origin.y + 30;
                    CGFloat yUpdate = self.isPresent ? yDistance : -yDistance;
                    frame.origin.y -= yUpdate;
                } else if (visibleCell.tag > self.selectClell.tag){
                    CGFloat yDistance = CGRectGetMaxY(self.finalFrame) - CGRectGetMaxY(self.originFrame) + 30;
                    CGFloat yUpdate = self.isPresent ? yDistance : -yDistance;
                    frame.origin.y += yUpdate;
                }
                visibleCell.frame = frame;
                visibleCell.transform = self.isPresent ? CGAffineTransformMakeScale(0.8, 1.0) : CGAffineTransformIdentity;
            }
        }
        self.selectClell.backButton.alpha = self.isPresent ? 1.0 : 0.0;
        self.selectClell.titleLine.alpha = self.isPresent ? 1.0 : 0.0;
        self.selectClell.textView.alpha = self.isPresent ? 1.0 : 0.0;
        self.selectClell.frame = self.isPresent ? self.finalFrame : self.originFrame;
        [self.selectClell layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        addView.hidden = YES;
        [transitionContext completeTransition:YES];
    }];
}


#pragma mark - UIViewControllerAnimatedTransitionDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPresent = YES;
    self.selectClell.textView.scrollEnabled = NO;
    
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresent = NO;
    
    return self;
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
    return self.interactionController;
}
如果有什么不正確的地方請指正
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 前言的前言 唐巧前輩在微信公眾號「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各項指標...
    VincentHK閱讀 5,432評論 3 44
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 持續一周的感冒總算好些了。儀式感都磨沒了。爬起來來組線條練習。 均臨摹于陽陽 希望2018舞起來 沒有基礎還是常練...
    肉肉太肉啊閱讀 667評論 8 8
  • 這是我第一次在這里寫文章,如果寫得不好的話,請大家多多包涵一下。 在XX網學完CSS3之后,然后我懂了一個道理,X...
    謝小豪閱讀 2,076評論 0 0
  • 文章摘要:——為節省您的時間,閱讀下面【277字】的摘要即可了解本文大意:本文首先通過幾個事例介紹了養育孩子的真實...
    石頭聊家庭教育閱讀 358評論 0 2