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

写这个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阅读 10,772评论 3 44
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,260评论 4 61
  • 持续一周的感冒总算好些了。仪式感都磨没了。爬起来来组线条练习。 均临摹于阳阳 希望2018舞起来 没有基础还是常练...
    肉肉太肉啊阅读 3,826评论 8 8
  • 这是我第一次在这里写文章,如果写得不好的话,请大家多多包涵一下。 在XX网学完CSS3之后,然后我懂了一个道理,X...
    谢小豪阅读 6,218评论 0 0
  • 文章摘要:——为节省您的时间,阅读下面【277字】的摘要即可了解本文大意:本文首先通过几个事例介绍了养育孩子的真实...
    石头聊家庭教育阅读 2,880评论 0 2