UIScrollView如何循环滚动?

9

可能是重复问题:
UIScrollView。如何实现“无限”滚动/缩放?

我有一个UIScrollView,在其中有不同的图片(约30张)。我想在用户到达最后一张图片时,显示其后面的第一张图片,依此类推。我还想用第一张图片实现相同的功能(跳转到最后一张)。 我希望平稳地循环这些图片,让用户甚至不会注意到他正在进行另一次循环。

如何最好地实现这个功能?

谢谢。


1
更好的描述方式应该是“循环”的(在我看来)。 - mk12
2个回答

24
我刚刚完成了这个任务。我有一个图片数组需要添加到scrollview中,首先我会先将最后一张图片添加到scrollview中,接着循环遍历整个数组将所有的图片添加进去,最后再将数组中的第一张图片再次添加到scrollview中。
请查看以下代码:
#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

我随后使用scrollViewDidEndDecelerating来检查用户滚动到何处,然后将他们跳转到第二张图片或倒数第二张图片,以便他们可以持续滚动...如果我没有解释清楚,很抱歉 - 时间不够!但是这段代码在我的设备上运行良好..


1
这行代码:imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); 应该修改为:imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + WIDTH_OF_IMAGE, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); - mattorb
1
嗨,Craig!非常感谢你的代码。 我正在尝试制作类似的东西,但是我想在滚动视图中看到几张图片(例如3张),而不是一个单独的图片大小。怎么做??你能帮我吗?谢谢 - Frade

7

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