标签:
/**
* 设置scrollView
*/
- (void)setUpScrollView:(NSArray *)array {
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:scrollView];
self.scrollView = scrollView;
}
/**
* 设置scrollView的滚动图片
*/
- (void)setUpImage:(NSArray *)array {
CGSize contentSize;
CGPoint startPoint;
for (int i = 0; i < array.count; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
imageView.image = [UIImage imageNamed:array[i]];
[self.scrollView addSubview:imageView];
contentSize = CGSizeMake(array.count * self.frame.size.width, 0);
startPoint = CGPointZero;
}
self.scrollView.contentSize = contentSize;
}
#pragma mark - scrollView代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScroll");
if (scrollView.contentOffset.x < 0) {
NSLog(@"%lf",scrollView.contentOffset.x);
NSLog(@"scrollView.contentOffset.x < self.frame.size.width");
[self.scrollView setContentOffset:CGPointMake(self.frame.size.width * self.imageArray.count, 0) animated:NO];
}
NSLog(@"%f",scrollView.contentOffset.x);
}
demo只实现向右滑动,不断播放的效果。来说一下,这段代码。[self.scrollView setContentOffset:CGPointMake(self.frame.size.width * self.imageArray.count, 0) animated:NO];这里为什么是 self.imageArray.count 而不是self.imageArray.count - 1呢,因为当我们滑动,这里重新赋值之后,还会执行向右滑动整个page的功能,所以这里是最后一张图片的下一个图片的起始点(其实根本没有这个图片,所以这里会有点卡)。而不是说我门滑动之后,就只执行赋值的代码就不执行别的代码了。具体的优化和完整的功能实现,会后续再写。
标签:
原文地址:http://my.oschina.net/u/2418942/blog/525331