2009-09-05 14 views
8

Powiel możliwe:
UIScrollView. Any thoughts on implementing “infinite” scroll/zoom?loop UIScrollView

Mam UIScrollView aw nim różnych obrazów (około 30). Chciałbym, aby było to możliwe, gdy użytkownik dojdzie do ostatniego obrazu, aby pokazać pierwszy po nim i tak dalej. I chcę zaimplementować tę samą funkcję z pierwszym obrazem (aby przejść do ostatniego). Chciałbym pętli obrazów płynnie, że użytkownik nawet nie zauważy, że robi kolejną pętlę.

Jaki jest najlepszy sposób, aby to osiągnąć?

Dzięki.

+1

Lepszym sposobem opisu byłoby * okrągłe * (IMO). – mk12

Odpowiedz

24

Właśnie to zrobiłem. Mam szereg obrazów do dodania do przewijania, dlatego najpierw dodaję ostatnią do widoku przewijania, następnie przechodzę przez tablicę, aby dodać wszystkie obrazy, a następnie dodam pierwszy z tablicy ponownie.

Sprawdźcie przez ten kod:

#import "MainViewController.h" 
#import "MainView.h" 

#define WIDTH_OF_SCROLL_PAGE 320 
#define HEIGHT_OF_SCROLL_PAGE 352 
#define WIDTH_OF_IMAGE 320 
#define HEIGHT_OF_IMAGE 352 
#define LEFT_EDGE_OFSET 0 

@implementation MainViewController 

@synthesize scrollView, slideImages; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    scrollView = [[UIScrollView alloc] init]; 
    CGRect scrollFrame; 
    scrollFrame.origin.x = 0; 
    scrollFrame.origin.y = 0; 
    scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE; 
    scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE; 

    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame]; 
    scrollView.bounces = YES; 
    scrollView.pagingEnabled = YES; 
    scrollView.delegate = self; 
    scrollView.userInteractionEnabled = YES; 

    slideImages = [[NSMutableArray alloc] init]; 
    [slideImages addObject:@"welcome-small.jpg"]; 
    [slideImages addObject:@"football-small.jpg"]; 
    [slideImages addObject:@"dancing-small.jpg"]; 
    [slideImages addObject:@"celebration-small.jpg"]; 

    //add the last image first 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]; 
    imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); 
    [scrollView addSubview:imageView]; 
    [imageView release]; 

    for (int i = 0;i<[slideImages count];i++) { 
     //loop this bit 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]]; 
     imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); 
     [scrollView addSubview:imageView]; 
     [imageView release]; 
     // 
    } 

    //add the first image at the end 
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]]; 
    imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); 
    [scrollView addSubview:imageView]; 
    [imageView release]; 

    [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)]; 
    [scrollView setContentOffset:CGPointMake(0, 0)]; 
    [self.view addSubview:scrollView]; 
    [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; 
    [super viewDidLoad]; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width/([slideImages count]+2))/self.scrollView.frame.size.width) + 1; 
    if (currentPage==0) { 
     //go last but 1 page 
     [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; 
    } else if (currentPage==([slideImages count]+1)) { 
     [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; 
    } 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [scrollView release]; 
    [slideImages release]; 
    [super dealloc]; 
} 


@end 

Następnie używać scrollViewDidEndDecelerating aby sprawdzić, gdzie w zwoju użytkownik jest, a następnie przeskoczyć je do 2 obrazu lub last but 1 obrazek aby mogli przewinąć ciągły ... Przepraszam, jeśli nie wyjaśniłem tego dobrze - niedługo! Ale ten kod działa dobrze na moim urządzeniu.

+1

Wiersz, który brzmi: 'imageView.frame = CGRectMake ((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);" powinien zmienić się na: 'imageView.frame = CGRectMake ((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + WIDTH_OF_IMAGE, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); ' – mattorb

+1

Witaj Craig! Dziękuję bardzo za twój kod. Próbuję zrobić coś podobnego do tego, ale zamiast przewijania wielkości pojedynczego obrazu chciałbym zobaczyć kilka obrazów (na przykład 3) w przewijanym widoku. Jak to zrobić? Możesz mi pomóc?? Dzięki – Frade