Can I animate the UIScrollView contentOffset property via its layer?

后端 未结 3 1064
梦如初夏
梦如初夏 2021-01-30 14:37

I want to zoom and scroll a UIScrollView with a CGPathRef. Because of that I assume I have to animate the UIScrollView\'s layer property? But which property would I animate th

3条回答
  •  孤城傲影
    2021-01-30 15:05

    You have to animate the bounds property. In fact, that's what the contentOffset property uses behind the scenes.

    Example:

    CGRect bounds = scrollView.bounds;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    animation.duration = 1.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    animation.fromValue = [NSValue valueWithCGRect:bounds];
    
    bounds.origin.x += 200;
    
    animation.toValue = [NSValue valueWithCGRect:bounds];
    
    [scrollView.layer addAnimation:animation forKey:@"bounds"];
    
    scrollView.bounds = bounds;
    

    If you're curious, the way I used to get this information is the following:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    
    [scrollView setContentOffset:CGPointMake(200, 0) animated:NO];
    
    [UIView commitAnimations];
    
    NSLog(@"%@",scrollView);
    

    The NSLog call will output:

    ; }; layer = ; contentOffset: {246, 0}>
    

    The animations snippet will list all the active animations, in this case { bounds=; }.

    Hope this helps.

提交回复
热议问题