2013-03-13 3 views
3

Je cette ScrollView:comment puis-je faire un défilement automatique 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); 

et je veux faire autoscroll très lentement vers le bas jusqu'à la fin afin que l'utilisateur peut voir le contenu (comme dans les crédits du film), éventuellement avec un bouton pour arrêter/jouer , mais pour suivre les gestes de l'utilisateur lorsque vous touchez l'interface.

Comment est-ce que je peux faire ceci? Merci,

Répondre

2

ce adapté le code a fait l'affaire (source http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)

PS: chaque image est de 280 par 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

Vous pouvez définir x si vous voulez faire défiler horizontalement, sinon y mettre à défilement vertical.

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

et de modifier les coordonnées en conséquence.

+0

merci, comment puis-je saisir un délai afin que l'utilisateur puisse voir le contenu jusqu'à cet emplacement? – Mihai

4

Vous pouvez utiliser:

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

et utiliser les x et y comme les points de contact sur l'écran, vous pouvez capturer.

Vous pouvez également faire une animation avec CoreAnimation:

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

désolé je suis un Newby, ce qui est x, y censé être changé? – Mihai

+0

les coordonnées sur l'écran x est la position horizontale de contact, y est la verticale. – ApolloSoftware

+0

Les valeurs valides sont 0..Largeur d'écran/Hauteur max. Par exemple, un iPad sans rétine est 768x1024 pour un plein écran en orientation portrait. Bricoler avec les valeurs et vous verrez. – ApolloSoftware

Questions connexes