如何使ScrollView自动滚动?

3

我有一个scrollView:

self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.scrollView.contentSize = CGSizeMake(320,3000);


_scrollView.frame = CGRectMake(0, 45, 320, 420);

我希望让页面内容自动向下缓慢滚动到底部,这样用户可以看到全部内容(就像电影字幕),并最终提供一个停止/播放按钮,但要根据用户触摸界面来进行操作。如何实现呢?

谢谢!

3个回答

4

您可以使用:

  [_scrollView setContentOffset:CGPointMake(x,y) animated:YES];

您可以使用屏幕上的x和y作为触点进行捕捉。

您还可以使用CoreAnimation进行动画:

 [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
 [UIScrollView setAnimationDuration:1.0f];
 [scroll setContentOffset:CGPointMake(x, y)];
 [UIScrollView commitAnimations];

屏幕上的坐标x是触摸的水平位置,y是垂直位置。 - apollosoftware.org
啊,抱歉,我的错。现在我明白了。我想表达的是,我希望它像电影最后的字幕一样自动缓慢滚动。 - Mihai
将持续时间设置为5.0f,我想那大概是5秒钟。或者您喜欢的任何数字。 - apollosoftware.org
[UIScrollView setAnimationDuration:5.0f]; [_scrollView setContentOffset:CGPointMake(0, 2500) animated:YES]; [UIScrollView commitAnimations]; 它能够到达位置,但没有延迟。当我打开它时,它已经在那个位置了。 - Mihai
删除animated:YES,因为你已经在使用CoreAnimation来完成它了,你需要重写。animated:YES将使用默认值而没有延迟。 - apollosoftware.org
显示剩余2条评论

2
这个改编后的代码解决了问题(来源:http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)。PS:每张图片大小为280 x 200。
  - (void)viewDidLoad
 {
[super viewDidLoad];

UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
 }

  - (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.

for (int i=1; i<=10; i++) {
    // create image
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.jpg",i]];
    // create imageView
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
    // set scale to fill
    imgV.contentMode=UIViewContentModeScaleToFill;
    // set image
    [imgV setImage:image];
    // apply tag to access in future
    imgV.tag=i+1;
    // add to scrollView
    [scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];


}

 - (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.y;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
// if page is not 10, display it
if( nextPage!=10 )  {


    [scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
    pgCtr.currentPage=nextPage;
    // else start sliding form 1 :)


} else {

    [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
    pgCtr.currentPage=0;
}
}

1
如果您想水平滚动,请设置x,否则设置y以垂直滚动。
[_scrollView setContentOffset:CGPointMake(x, y) animated:YES];   

并相应地修改坐标。

谢谢,我该如何输入延迟以便用户可以看到该位置的内容? - Mihai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接