标签:
思想: 在整个相册的最前面放一张最后的照片,在整个相册的最后面放一张开始的照片(例如,相册中总共有七张照片,分别是h1.jpg—h7.jpg。为了做循环相册,现在在最前面添加一张h7,最后面添加一张h1,变成了9张照片。)。用一个UIScrollView的对象来控制照片的滑动,当滑到第九张(最后的那张h1)时瞬间将它的偏移量变成(Width, 0),这样它实际上是自己又跳转到自己的界面,一般是看不出来的。当往右滑动时,当滑动到第一张(此时的偏移量是((0, 0))瞬间将偏移量变成(7 * Width, 0),这样它就瞬间移动到原来相册的最后一张了。
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
[super loadView];
//初始化
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
_scrollView.contentSize = CGSizeMake(9 * Width, Height);
//偏移量初始时刻为(Width, 0)
_scrollView.contentOffset = CGPointMake(Width, 0);
_scrollView.pagingEnabled = YES;
//签协议
_scrollView.delegate = self;
//第二步:设置图片(将相册添加到_scrollView上)
UIImageView *imageViewFront = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)];
imageViewFront.image = [UIImage imageNamed:@"h7.jpg"];
[_scrollView addSubview:imageViewFront];
//使照片以最合适的尺寸呈现
imageViewFront.contentMode = UIViewContentModeScaleAspectFit;
[imageViewFront release];
for (NSInteger i = 1; i <= 7; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * Width, 0, Width, Height)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpg", i]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[_scrollView addSubview:imageView];
[imageView release];
}
UIImageView *imageViewLast = [[UIImageView alloc]initWithFrame:CGRectMake(8 * Width, 0, Width, Height)];
imageViewLast.image = [UIImage imageNamed:@"h1.jpg"];
imageViewLast.contentMode = UIViewContentModeScaleAspectFit;
[_scrollView addSubview:imageViewLast];
[imageViewLast release];
//第三步:将_scrollView添加到self.view上
[self.view addSubview:_scrollView];
[_scrollView release];
}
//签了协议后,滚动视图时所触发的方法。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (_scrollView.contentOffset.x >= 8 * Width) {
_scrollView.contentOffset = CGPointMake(Width, 0);
}else if(_scrollView.contentOffset.x < 0){
_scrollView.contentOffset = CGPointMake(7 * Width, 0);
}
}
标签:
原文地址:http://www.cnblogs.com/kevinDong/p/4779899.html