Animating UICollectionView contentOffset does not display non-visible cells

后端 未结 7 2102
旧巷少年郎
旧巷少年郎 2021-02-01 18:31

I\'m working on some ticker-like functionality and am using a UICollectionView. It was originally a scrollView, but we figure a collectionView will make it easier

7条回答
  •  孤独总比滥情好
    2021-02-01 19:02

    You could try using a CADisplayLink to drive the animation yourself. This is not too hard to set up since you are using a Linear animation curve anyway. Here's a basic implementation that may work for you:

    @property (nonatomic, strong) CADisplayLink *displayLink;
    @property (nonatomic, assign) CFTimeInterval lastTimerTick;
    @property (nonatomic, assign) CGFloat animationPointsPerSecond;
    @property (nonatomic, assign) CGPoint finalContentOffset;
    
    -(void)beginAnimation {
        self.lastTimerTick = 0;
        self.animationPointsPerSecond = 50;
        self.finalContentOffset = CGPointMake(..., ...);
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)];
        [self.displayLink setFrameInterval:1];
        [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    
    -(void)endAnimation {
        [self.displayLink invalidate];
        self.displayLink = nil;
    }
    
    -(void)displayLinkTick {
        if (self.lastTimerTick = 0) {
            self.lastTimerTick = self.displayLink.timestamp;
            return;
        }
        CFTimeInterval currentTimestamp = self.displayLink.timestamp;
        CGPoint newContentOffset = self.collectionView.contentOffset;
        newContentOffset.x += self.animationPointsPerSecond * (currentTimestamp - self.lastTimerTick)
        self.collectionView.contentOffset = newContentOffset;
    
        self.lastTimerTick = currentTimestamp;
    
        if (newContentOffset.x >= self.finalContentOffset.x)
            [self endAnimation];
    }
    

提交回复
热议问题