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.
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
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With