UIScrollView scrollRectToVisible at custom speed

前端 未结 2 1396
我在风中等你
我在风中等你 2020-12-29 15:39

I have a UIScrollView and I\'m calling scrollRectToVisible:animated:YES on it. I would like to set the speed at which it is animated. Can that be done?

相关标签:
2条回答
  • 2020-12-29 16:21

    I ended up finding a solution. In my case, the scrolling was animated programmatically after launch, to mimic a slot machine (with 3 horizontal UIScrollViews). Was doing this with the scrollRectToVisible:animated: method.

    I got to set a custom speed using UIView's beginAnimation:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
    scrollMid.contentOffset = CGPointMake(rMid*320, 0);
    [UIView commitAnimations];
    

    AnimationDuration depends on the distance the scroller has to move between each "drawing".

    0 讨论(0)
  • 2020-12-29 16:29

    A modern version with blocks:

    [UIView animateWithDuration:1.0 animations:^{
        [self.scrollView scrollRectToVisible:CGRectMake(...) animated:NO];
    } completion:^(BOOL finished) {
        ...
    }];
    
    0 讨论(0)
提交回复
热议问题