How to properly animate UIScrollView contentOffset

前端 未结 3 894
暗喜
暗喜 2020-12-08 10:59

I have UIScrollView subclass. Its content is reusable - about 4 or 5 views are used to display hundreds of elements (while scrolling hidden objects reused and jumps to anoth

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 11:43

    A nice way to do this is with the AnimationEngine library. It's a very small library: six files, with three more if you want damped spring behavior.

    Behind the scenes it uses a CADisplayLink to run your animation block once every frame. You get a clean block-based syntax that's easy to use, and a bunch of interpolation and easing functions that save you time.

    To animate contentOffset:

    startOffset = scrollView.contentOffset;
    endOffset = ..
    
    // Constant speed looks good...
    const CGFloat kTimelineAnimationSpeed = 300;
    CGFloat timelineAnimationDuration = fabs(deltaToDesiredX) / kTimelineAnimationSpeed;
    
    [INTUAnimationEngine animateWithDuration:timelineAnimationDuration
                                       delay:0
                                      easing:INTULinear
                                  animations:^(CGFloat progress) {
                                        self.videoTimelineView.contentOffset = 
                                             INTUInterpolateCGPoint(startOffset, endOffset, progress);
                                  }
                                 completion:^(BOOL finished) {
                                       autoscrollEnabled = YES;
                                 }];
    

提交回复
热议问题