图片轮播器 在很多应用软件中都有应用 ,需要制作 无限滚动的 ,下面贴上自己写的小Demo, 能实现相关的功能。
Ps: 没有用到pageControl ,只是图片自动无限滚动 ,没有任何的 处理 。
#import"ViewController.h"
@interfaceViewController :UIViewController<UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView*scrollView;
@property(nonatomic,weak)UIImageView*currentImageView;//当前imageView
@property(nonatomic,weak)UIImageView*nextImageView;//下一个imageView
@property(nonatomic,weak)UIImageView*preImageView;//上一个imageView
@property(nonatomic,assign)BOOLisDragging;//是否正在拖动
@property(nonatomic,strong)NSTimer*timer;//设置动画
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView*scrollView =[[UIScrollViewalloc]init];
CGFloatwidth =self.view.frame.size.width;
CGFloatheight =self.view.frame.size.height;
scrollView.frame=CGRectMake(0,0, width, height);
[self.viewaddSubview:scrollView];
self.scrollView= scrollView;
[self.scrollViewsetContentSize:CGSizeMake(width *3, height)];
//设置隐藏横向条
self.scrollView.showsHorizontalScrollIndicator=NO;
//设置自动分页
self.scrollView.pagingEnabled=YES;
//设置代理
self.scrollView.delegate=self;
//设置当前点
self.scrollView.contentOffset=CGPointMake(width,0);
//设置是否有边界
self.scrollView.bounces=NO;
//初始化当前视图
UIImageView*currentImageView =[[UIImageViewalloc]init];
currentImageView.image= [UIImageimageNamed:@"1.jpg"];
[self.scrollViewaddSubview:currentImageView];
self.currentImageView= currentImageView;
self.currentImageView.frame=CGRectMake(width,0, width, height);
self.currentImageView.contentMode=UIViewContentModeScaleAspectFill;
//初始化下一个视图
UIImageView*nextImageView = [[UIImageViewalloc]init];
nextImageView.image= [UIImageimageNamed:@"2.jpg"];
[self.scrollViewaddSubview:nextImageView];
self.nextImageView= nextImageView;
self.nextImageView.frame=CGRectMake(width *2,0, width, height);
self.nextImageView.contentMode=UIViewContentModeScaleAspectFill;
//初始化上一个视图
UIImageView*preImageView =[[UIImageViewalloc]init];
preImageView.image= [UIImageimageNamed:@"6.jpg"];
preImageView.frame=CGRectMake(0,0, width, height);
[self.scrollViewaddSubview:preImageView];
self.preImageView= preImageView;
self.preImageView.contentMode=UIViewContentModeScaleAspectFill;
//设置时钟动画定时器
self.timer= [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(update:)userInfo:nilrepeats:YES];
//将定时器添加到主线程
[[NSRunLoopmainRunLoop]addTimer:self.timerforMode:NSRunLoopCommonModes];
}
- (void)update:(NSTimer*)timer{
//定时移动
if(_isDragging==YES) {
return;
}
CGPointoffSet =self.scrollView.contentOffset;
offSet.x+=offSet.x;
[self.scrollViewsetContentOffset:offSetanimated:YES];
if(offSet.x>=self.view.frame.size.width*2) {
offSet.x=self.view.frame.size.width;
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
_isDragging=YES;
}
//停止滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
_isDragging=NO;
step=0;
}
//开始拖动
- (void) scrollViewDidScroll:(UIScrollView*)scrollView{
staticinti =1;//当前展示的是第几张图片
floatoffset =self.scrollView.contentOffset.x;
if(self.nextImageView.image==nil||self.preImageView.image==nil) {
//加载下一个视图
NSString*imageName1 = [NSStringstringWithFormat:@"%d.jpg",i ==KOUNT?1:i +1];
_nextImageView.image= [UIImageimageNamed:imageName1];
//加载上一个视图
NSString*imageName2 = [NSStringstringWithFormat:@"%d.jpg",i==1?KOUNT:i-1];
_preImageView.image= [UIImageimageNamed:imageName2];
}
if(offset ==0){
_currentImageView.image=_preImageView.image;
scrollView.contentOffset=CGPointMake(scrollView.bounds.size.width,0);
_preImageView.image=nil;
if(i ==1) {
i =KOUNT;
}else{
i-=1;
}
}
if(offset == scrollView.bounds.size.width*2) {
_currentImageView.image=_nextImageView.image;
scrollView.contentOffset=CGPointMake(scrollView.bounds.size.width,0);
_nextImageView.image=nil;
if(i ==KOUNT) {
i=1;
}else{
i +=1;
}
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end