在CoreAnimation期间重置UIScrollView contentOffset

时间:2013-04-23 22:57:09

标签: iphone ios objective-c uiscrollview core-animation

我有一个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,并无缝地继续动画。 / p>

当我尝试在

中执行此操作时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 600)
    {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
}

在动画期间似乎没有调用该方法。尝试在scrollView上使用键值观察器时会发生同样的情况,手动滚动时调用方法会很好,但在coreAnimation期间则不会。使用NSTimer方法定期检查也证明没有结果。

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

1 个答案:

答案 0 :(得分:2)

UIScrollViews实际上不会在动画期间更改其偏移量。如果你想“重置”偏移量,我建议你弄清楚如何在正确的时间结束你的动画,然后设置界限。

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