2013-03-13 50 views
3

我有這樣的滾動視圖:我怎樣才能使scrollView autoscroll?

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); 

,我希望把它向下自動滾動速度非常慢到年底,使用戶可以看到的內容(如電影學分),最終與鍵停止/播放,但在觸摸界面時遵循用戶手勢。

我該怎麼做? 謝謝,

回答

2

這適於代碼並獲得成功(源http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/

PS:每個圖像是280由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]; 

並相應地修改座標。

+0

感謝,我如何輸入一個延遲,以便用戶可以看到該位置的內容? – Mihai 2013-03-13 21:00:10

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]; 
+0

對不起,我是一個newby,什麼是x,y應該改變? – Mihai 2013-03-13 20:30:10

+0

屏幕上的座標x是觸摸的水平位置,y是垂直位置。 – ApolloSoftware 2013-03-13 20:33:15

+0

有效的值是0 ..最大屏幕寬度/高度。例如,沒有視網膜的iPad在縱向全屏時爲768x1024。修改這些值,你會看到。 – ApolloSoftware 2013-03-13 20:35:32