思路:比需要创建的滚动图片多添加两个,在第一个图片前面把最后个添加进去,最后面个图片后面添加第一个图片。
1、在第一个显示的图片前面添加最后一张图片
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
button0.frame = CGRectMake(0, 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);
if (modelArray.count > 0) {
AdvertisementModel *model = [modelArray objectAtIndex:modelArray.count - 1];
//不为空
if (![model.imagehttp isEqual:[NSNull null]]) {
ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];
UIImage *img = [UIImage imageWithData:ImgData];
[button0 setBackgroundImage:img forState:UIControlStateNormal];
}else
{
[button0 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d.png",modelArray.count]] forState:UIControlStateNormal];
}
button0.tag = 99;
[button0 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[__scrollerView addSubview:button0];
}
2、添加需要添加的图片
for (int i = 1; i <= modelArray.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(i*kScreenwidth, 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);
AdvertisementModel *model = [modelArray objectAtIndex:i - 1];
//不为空
if (![model.imagehttp isEqual:[NSNull null]]) {
ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];
UIImage *img = [UIImage imageWithData:ImgData];
[button setBackgroundImage:img forState:UIControlStateNormal];
}else
{
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d.png",i]] forState:UIControlStateNormal];
}
button.tag = 100 + i;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[__scrollerView addSubview:button];
}
3、在最后面张图片后面添加第一张图
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(kScreenwidth * (modelArray.count + 1), 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);
if (modelArray.count > 0) {
AdvertisementModel *model = [modelArray objectAtIndex:0];
//不为空
if (![model.imagehttp isEqual:[NSNull null]]) {
ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];
UIImage *img = [UIImage imageWithData:ImgData];
[button1 setBackgroundImage:img forState:UIControlStateNormal];
}else
{
[button1 setBackgroundImage:[UIImage imageNamed:@"pic1.png"] forState:UIControlStateNormal];
}
button1.tag = 98;
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[__scrollerView addSubview:button1];
}
4、设置偏移量 指向第一张图
__scrollerView.contentOffset = CGPointMake(kScreenwidth, 0);
//代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSInteger page = (NSInteger)scrollView.contentOffset.x /kScreenwidth - 1;
_pageControl.currentPage = page;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger page = (NSInteger)scrollView.contentOffset.x /kScreenwidth;
if (page == 0) {
[__scrollerView scrollRectToVisible:CGRectMake(kScreenwidth*modelArray.count, 0, kScreenwidth, 256) animated:NO];
}
else if (page == modelArray.count + 1)
{
[__scrollerView scrollRectToVisible:CGRectMake(kScreenwidth, 0, kScreenwidth, 256) animated:NO];
}
}