2013-04-23 67 views
0

期间复位的UIScrollView contentOffset我有一个UIScrollView被垂直coreAnimation滚动如下: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上使用Key-Value观察者时,会发生同样的情况,手动滚动时该方法调用得很好,但在coreAnimation期间调用方法不错。定期检查使用NSTimer方法也证明是徒劳的。

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

回答

2

UIScrollViews在动画过程中实际上不会改变它们的偏移量。如果你想“重置”偏移量,我会建议找出如何在正确的时间结束动画,然后设置边界。

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

+0

我认为可能是这种情况。然而,在触摸事件的正常滚动期间(即使正在进行动量滚动),这些值是可访问的吗?什么是完成这种动画的最简单的方法,即。一个垂直滚动数字自动收报机?目前我正在考虑通过非常小的线性值来动画它,并通过缓动函数计算持续时间值,但这似乎不是实现它的最佳方式。 – stefmalawi 2013-04-24 00:43:34

+0

UIScrollView真的不是为此而构建的。你可以看看这里的一些答案:http://stackoverflow.com/questions/5675535/how-to-implement-cyclic-scrolling-on-tableview,但我不认为你会最终以一个伟大的解。 – 2013-04-24 16:39:15