iOS:一些交互效果的实现

2019-02-22  本文已影响5人  HoooChan

PageView头部滚动

顶部有一个头部,底下是Pages包含TableView的场景很常见到,实现的方法也有很多种:

一、监听当前显示的tableView的contentOffset,当header还没到顶时,拿原本要移动的offset使header移动,然后恢复tableView的contentOffset为旧值。

二、重写tableView的shouldRecognizeSimultaneouslyWithGestureRecognizer返回YES,给root容器添加手势,处理整个页面的滑动以及控制tableVIew是否应该滑动。当开始点击View时保存当前tableView的contentOffset,如果监听到contentOffset改变而此时tableView不应该滚动,则恢复到之前保存的值。

三、还有一种非常简单的方法,将所有tableView的contentInsets的top设置为header的高度,监听tableView的contentOffset并修改header的frame,看起来header也在滚动。还可以设置tableView的scrollIndicatorInsets,足以以假乱真!下面是处理header滚动的方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (object == _currentTableView) {
        CGPoint oldContentOffset = [change[NSKeyValueChangeOldKey] CGPointValue];
        CGPoint newContentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
        
        BOOL shouldScrollHeaderView = NO;
        if (newContentOffset.y > oldContentOffset.y) {
            shouldScrollHeaderView = YES;
        }else {
            if (newContentOffset.y + _headerHeight < -self.headerView.frame.origin.y) {
                shouldScrollHeaderView = YES;
            }
        }
        
        if (shouldScrollHeaderView) {
            CGRect oldHeaderFrame = self.headerView.frame;
            CGRect newHeaderFrame = self.headerView.frame;
            
            newHeaderFrame.origin.y -= newContentOffset.y - oldContentOffset.y;
            if (newHeaderFrame.origin.y < -_headerHeight + _marginTop) {
                newHeaderFrame.origin.y = -_headerHeight + _marginTop;
            }
            self.headerView.frame = newHeaderFrame;
            for (UIScrollView *otherTableView in _tableViews) {
                if (otherTableView != _currentTableView) {
                    CGPoint  tmpContentOffset = otherTableView.contentOffset;
                    tmpContentOffset.y += oldHeaderFrame.origin.y - self.headerView.frame.origin.y;
                    otherTableView.contentOffset = tmpContentOffset;
                }
            }
        }

    }
}

Ramotion/expanding-collection

关键代码:
1、设置UICollectionView的contentInset和contentOffset,使第一个cell在屏幕居中;
2、复写UICollectionViewFlowLayout的targetContentOffsetForProposedContentOffset方法,使得滚动结束后恰好停止在下一个cell居中的位置:

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
    CGRect proposedRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    
    CGFloat proposedContentOffsetCenterX = proposedContentOffset.x + self.collectionView.bounds.size.width / 2.0;
    UICollectionViewLayoutAttributes *centerNearestAttributes;
    
    NSArray *layoutAtrributesArray = [self layoutAttributesForElementsInRect:proposedRect];
    for (UICollectionViewLayoutAttributes *layoutAtrributes in layoutAtrributesArray) {
        if (layoutAtrributes.representedElementCategory != UICollectionElementCategoryCell) {
            continue;
        }
        
        if (!centerNearestAttributes) {
            centerNearestAttributes = layoutAtrributes;
            continue;
        }
        
        if (fabs(layoutAtrributes.center.x - proposedContentOffsetCenterX) < fabs(centerNearestAttributes.center.x - proposedContentOffsetCenterX)) {
            centerNearestAttributes = layoutAtrributes;
        }
    }
    
    CGFloat newOffsetX = centerNearestAttributes.center.x - self.collectionView.bounds.size.width / 2.0;
    CGFloat offset = newOffsetX - self.collectionView.contentOffset.x;
    
    if ((velocity.x < 0 && offset > 0) || (velocity.x > 0 && offset < 0)) {
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
        CGFloat pageWidth = layout.itemSize.width + layout.minimumLineSpacing;
        newOffsetX += velocity.x > 0 ? pageWidth : -pageWidth;
    }
    return CGPointMake(newOffsetX, proposedContentOffset.y);
}

3、滚动时cell的变化在layoutAttributesForElementsInRect中修改自带的属性,别忘了重写shouldInvalidateLayoutForBoundsChange返回YES:

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    CGRect visibleRect = CGRectMake(self.collectionView.bounds.origin.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    CGFloat visibleCenterX = CGRectGetMidX(visibleRect);
    
    NSArray *superAttributeArray = [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes *layoutAtrributes in superAttributeArray) {
        CGFloat distanceFtomCenter = fabs(visibleCenterX - layoutAtrributes.center.x);
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
        CGFloat maxDistance = layout.itemSize.width + layout.minimumLineSpacing;
        distanceFtomCenter = fmin(distanceFtomCenter, maxDistance);
        CGFloat scale = 1 - distanceFtomCenter / maxDistance * 0.1;
        layoutAtrributes.transform3D = CATransform3DScale(CATransform3DIdentity, scale, scale, 1);
        CGFloat alpha = 1 - distanceFtomCenter / maxDistance * 0.7;
        layoutAtrributes.alpha = alpha;
    }
    return superAttributeArray;
}

4、push到新的ViewController vcB时,先copy当前的cell把它添加到view里面,先拿到vcB的截图做动画,动画结束后再push,且这时的animate设为NO;

Ramotion/cardslider

首先要把pageEnabled打开,然后根据当前的pageNumber来计算显示出来的cell的位置,先计算固定不动时的位置,再根据滑到下一页的偏移量来确定滚动时的位置。

上一篇下一篇

猜你喜欢

热点阅读