水平滚动视图在几秒钟后自动滚动

时间:2011-11-03 08:46:51

标签: iphone uiscrollview

我有一个水平scrollView,其中包含2张图片。scrollView是圆形的。我想要做的是在几秒钟后设置自动滚动。任何想法我该怎么做?< / p>

我尝试了以下内容:

[UIScrollView beginAnimations:@"scrollAnimation" context:nil];
    [UIScrollView setAnimationDuration:5];
    [UIScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0)];
    [UIScrollView commitAnimations];

但是这个:[UIScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0)];无法识别。任何想法?谢谢

2 个答案:

答案 0 :(得分:1)

你误解了动画声明语法 我们建议您在代码中引用滚动视图,如下所示:

UIScrollView *scrollView = ...; // it must have a reference to the actual scroll view instance
...
[UIView beginAnimations: @"scrollAnimation" context:nil];
[UIView setAnimationDuration: 5.0f];

[scrollView setContentOffset:CGPointMake(x,y)];

[UIView commitAnimations];

这应该对你有用

答案 1 :(得分:1)

做这样的事情: - )

   [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

     - (void) onTimer {

       // Updates the variable h, adding 100 (put your own value here!)
      h += 100; 

      //This makes the scrollView scroll to the desired position  
      yourScrollView.contentOffset = CGPointMake(0, h);  

    }