我有一个UIScrollView与coreAnimation垂直滚动,如下所示:

 - (void) scrollAnimation
{
    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.y += 1000;

    animation.toValue = [NSValue valueWithCGRect:bounds];

    [scrollview.layer addAnimation:animation forKey:@"bounds"];

    scrollview.bounds = bounds;
}


scrollView的实际高度小于整个动画的高度,因此,当滚动视图达到其高度的末端时,我希望将contentOffset重置为0,然后无缝地继续动画。

当我尝试在内部执行此操作时

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 600)
    {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
}


动画期间似乎没有调用该方法。尝试在scrollView上使用键-值观察器时,也会发生相同的情况,该方法在手动滚动时可以很好地调用,但在coreAnimation期间不可以。使用NSTimer方法定期检查也被证明是徒劳的。

如何在contentOffset期间获取scrollview.bounds(或coreAnimation)并将其重置?
我希望为scrollView设置动画,以便内容(几个UILabels)在大于内容大小的动画上无缝滚动。

最佳答案

UIScrollViews实际上不会在动画过程中更改其偏移量。如果要“重置”偏移,我建议您弄清楚如何在正确的时间结束动画并随后设置边界。

您也可以使用UIScrollView的setContentOffset:animated:方法。您不会对动画有太多的控制权,但是在动画滚动和完成时会收到通知。

10-07 19:43
查看更多