通过用户交互循环UIScrollView

时间:2010-07-15 14:09:15

标签: iphone cocoa-touch animation scroll uiscrollview

我有一个循环scrollView,但我希望用户能够通过与scrollView交互(即用他们的手指滚动)来加速滚动,如果他们选择的话。我需要做额外的工作来实现这一点,因为动画滚动优先于用户交互并且显着减慢向下滚动。有没有人做过类似的事情?作为一个例子,我希望在愤怒的小鸟游戏的屏幕上使用类似的效果吗?

非常感谢

这里是滚动视图的basice动画代码

[UIView beginAnimations:@"scroll " context:nil];
[UIView setAnimationDuration:15];

[scrollView setContentOffset:CGPointMake(0, 600) animated:NO];
[UIView commitAnimations];

一旦用户尝试滚动,scrollView会稍微滚动(它会稍微移动),然后停止用户滚动操作并继续动画滚动?所以整体外观如下:

1.动画卷轴 2.用户尝试滚动并且动画似乎坚持 3.当用户结束滚动动画时继续  有什么想法吗?

再次感谢。

1 个答案:

答案 0 :(得分:1)

如果我理解你想说的话,你想要一个连续的慢速滚动(每15秒600px),这可以被用户否决。

如果这是你想要的,我会采取与此不同的方法。你正在开始拍摄的动画,可能只是按照它所说的那样做,而用户也在互动,这一切都会变得非常混乱。

创建一个NSTimer来处理慢速滚动动作,一次一个像素。然后用户仍然可以自由交互。

类似的东西:

...
NSTimer *slow = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollALittle:) userInfo:nil repeats:YES];
...
// maybe, somewhere:
[slow invalidate];

-(void)scrollALittle:(NSTimer*)theTimer
{
    CGPoint offset = scrollview.contentOffset;
    offset.y+=4; // 10 times 4 pix a second makes 600px/15 seconds.
    // add check on max y?

    // these checks are deliberately placed as close as possible to the contentOffset assignment
    if ( scrollview.tracking ) return;
    if ( scrollview.dragging ) return;
    if ( scrollview.zooming ) return;
    if ( scrollview.decelerating ) return;

    scrollview.contentOffset = offset;
}

当然,你可以在这里做很多事情,但这可能是一个起点。