圖片輪播器 在很多應(yīng)用軟件中都有應(yīng)用 遣鼓,需要制作 無限滾動的 啸盏,下面貼上自己寫的小Demo, 能實現(xiàn)相關(guān)的功能骑祟。
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;//設(shè)置動畫
@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)];
//設(shè)置隱藏橫向條
self.scrollView.showsHorizontalScrollIndicator=NO;
//設(shè)置自動分頁
self.scrollView.pagingEnabled=YES;
//設(shè)置代理
self.scrollView.delegate=self;
//設(shè)置當前點
self.scrollView.contentOffset=CGPointMake(width,0);
//設(shè)置是否有邊界
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;
//設(shè)置時鐘動畫定時器
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;
}
//停止?jié)L動
- (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