Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UIScrollView contentOffset animation speed?

Is there a way to change contentOffset animation speed without creating your own animation that sets the contentOffset?

The reason why I can't create my own animation for contentOffset change is that this will not call -scrollViewDidScroll: in regular intervals during animation.

like image 826
openfrog Avatar asked Mar 03 '13 19:03

openfrog


2 Answers

Unfortunately there is no clean and easy way to do that. Here is a slightly brutal, but working approach:

1) Add CADisplayLink as a property:

@property (nonatomic, strong) CADisplayLink *displayLink;

2) Animate content offset:

CGFloat duration = 2.0;

// Create CADisplay link and add it to the run loop
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_displayLinkTick)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[UIView animateWithDuration:duration animations:^{
    self.scrollView.contentOffset = newContentOffset;       
} completion:^(BOOL finished) {
    // Cleanup the display link
    [self.displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    self.displayLink = nil;
}];

3) Finally observe the changes on presentationLayer like:

- (void)_displayLinkTick {
    CALayer *presentationLayer = (CALayer *)self.scrollView.layer.presentationLayer;
    CGPoint contentOffset = presentationLayer.bounds.origin;
    [self _handleContentOffsetChangeWithOffset:contentOffset];
}

- (void)_handleContentOffsetChangeWithOffset:(CGPoint)offset {
    // handle offset change
}
like image 95
kkodev Avatar answered Oct 05 '22 11:10

kkodev


To get periodic information about the scroll state, you could run the animation in steps. The delegate will get called once (scrollViewDidScroll:) for each step

- (void)scrollTo:(CGPoint)offset completion:(void (^)(BOOL))completion {

    // this presumes an outlet called scrollView.   You could generalize by passing
    // the scroll view, or even more generally, place this in a UIScrollView category
    CGPoint contentOffset = self.scrollView.contentOffset;

    // scrollViewDidScroll delegate will get called 'steps' times
    NSInteger steps = 10;
    CGPoint offsetStep = CGPointMake((offset.x-contentOffset.x)/steps, (offset.y-contentOffset.y)/steps);
    NSMutableArray *offsets = [NSMutableArray array];

    for (int i=0; i<steps; i++) {
        CGFloat stepX = offsetStep.x * (i+1);
        CGFloat stepY = offsetStep.y * (i+1);
        NSValue *nextStep = [NSValue valueWithCGPoint:CGPointMake(contentOffset.x+stepX, contentOffset.y+stepY)];
        [offsets addObject:nextStep];
    }
    [self scrollBySteps:offsets completion:completion];
}

// run several scroll animations back-to-back
- (void)scrollBySteps:(NSMutableArray *)offsets completion:(void (^)(BOOL))completion {

    if (!offsets.count) return completion(YES);

    CGPoint offset = [[offsets objectAtIndex:0] CGPointValue];
    [offsets removeObjectAtIndex:0];

    // total animation time == steps * duration.  naturally, you can fool with both
    // constants.  to keep the rate constant, set duration == steps * k, where k
    // is some constant time per step
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.scrollView.contentOffset = offset;
    } completion:^(BOOL finished) {
        [self scrollBySteps:offsets completion:completion];
    }];
}

Call it like this...

CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);    
[self scrollTo:bottomOffset completion:^(BOOL finished) {}];

// BONUS completion handler!  you can omit if you don't need it
like image 21
danh Avatar answered Oct 05 '22 11:10

danh