印象笔记转场和弹簧特效的实现

作者: 骁驰 | 来源:发表于2015-11-13 01:31 被阅读506次

    写这个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;
    }
    
    如果有什么不正确的地方请指正

    相关文章

      网友评论

        本文标题:印象笔记转场和弹簧特效的实现

        本文链接:https://www.haomeiwen.com/subject/nfxhhttx.html