我有一个使用 coreAnimation 垂直滚动的 UIScrollView
,如下所示:
- (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
的实际高度小于整个动画,所以我希望在 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
)在大于内容大小的动画上无缝滚动。
最佳答案
UIScrollView 在动画期间实际上并不会改变它们的偏移量。如果您想“重置”偏移量,我建议您弄清楚如何在正确的时间结束动画并在之后设置边界。
您还可以使用 UIScrollView 的 setContentOffset:animated:
方法。您无法对动画进行太多控制,但您会在动画滚动和结束时收到通知。
关于iphone - 在 CoreAnimation 期间重置 UIScrollView contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16180531/