雖然基本還是用到第三方棒旗,但是寫一些簡單的廣告欄循環(huán)的話喘批,還是要會寫。知道基本的原理铣揉。
本文寫一個(gè)簡單的Demo
因?yàn)榧虞d的圖片基本都是數(shù)據(jù)庫的圖片饶深,所以首先要有SDWebImage,然后添加頭文件#import "UIImageView+WebCache.h"
簡單的寫兩個(gè)宏
#define UIScreen_width ([UIScreen mainScreen].bounds.size.width)
#define UIScreen_height ([UIScreen mainScreen].bounds.size.height)
添加代理<UIScrollViewDelegate>
然后定義變量
@property (strong, nonatomic) NSMutableArray *imageArray;//存放圖片的數(shù)組
@property (strong, nonatomic) UIScrollView *ADScroll;//廣告欄的底層ScrollView
@property (strong, nonatomic) NSTimer *timer;//計(jì)時(shí)器
開始在.m中完成簡單的Demo
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_imageArray = [NSMutableArray array];
[self createAD];
}
-(void)createAD{
_ADScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, UIScreen_width, UIScreen_height/3)];
_ADScroll.delegate = self;
[self.view addSubview:_ADScroll];
[self addImagesToScrollView];//添加圖片
[self createTimer];//創(chuàng)建計(jì)時(shí)器
}
-(void)addImagesToScrollView{
//這里隨便在網(wǎng)上搜了四張圖片存放在圖片數(shù)組中
NSArray *arr = @[@"http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg",@"http://pic25.nipic.com/20121112/5955207_224247025000_2.jpg",@"http://img10.3lian.com/c1/newpic/10/08/04.jpg",@"http://img3.imgtn.bdimg.com/it/u=2699593702,2049257415&fm=206&gp=0.jpg"];
[_imageArray addObjectsFromArray:arr];
int i = 0;
for (; i < _imageArray.count; i++) {
UIImageView *img = [[UIImageView alloc] init];
img.frame = CGRectMake(UIScreen_width * (i + 1), 50, UIScreen_width, UIScreen_height/3);
[img sd_setImageWithURL:[NSURL URLWithString:arr[i]]];
img.tag = i;
[_ADScroll addSubview:img];
img.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[img addGestureRecognizer:tap];
}
// 將最后一張圖片弄到第一張的位置
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, UIScreen_width, UIScreen_height/3)];
[img sd_setImageWithURL:[NSURL URLWithString:_imageArray[i-1]]];
[_ADScroll addSubview:img];
// 將第一張圖片放到最后位置逛拱,造成視覺上的循環(huán)
UIImageView *img0 = [[UIImageView alloc] initWithFrame:CGRectMake(UIScreen_width * (i + 1), 50, UIScreen_width, UIScreen_height/3)];
[img0 sd_setImageWithURL:[NSURL URLWithString:_imageArray[0]]];
[_ADScroll addSubview:img0];
[_ADScroll setContentOffset:CGPointMake(UIScreen_width, 0)];//將起始位置設(shè)置在這里
_ADScroll.pagingEnabled = YES;
_ADScroll.scrollEnabled = YES;
_ADScroll.showsHorizontalScrollIndicator = NO;
_ADScroll.showsVerticalScrollIndicator = NO;
_ADScroll.contentSize = CGSizeMake((i+2)*UIScreen_width, UIScreen_height/3);
}
#pragma mark - 給每一張imageView添加點(diǎn)擊事件
-(void)tapClick:(UITapGestureRecognizer *)tap{
UIImageView *image = (UIImageView *)tap.view;
int a = (int)image.tag;
NSLog(@"a = %i ",a);
// NSLog(@"111111");
}
#pragma mark - NSTimer
-(void)createTimer{
if (!_timer) {
_timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
}
-(void)timerRunning{
float Offx = _ADScroll.contentOffset.x;
Offx += UIScreen_width;
[_ADScroll setContentOffset:CGPointMake(Offx, 0) animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (_ADScroll == scrollView) {
//往左滑
if (scrollView.contentOffset.x >= scrollView.contentSize.width - UIScreen_width ) {
scrollView.contentOffset = CGPointMake(UIScreen_width, 0);
}
//往右滑
if (scrollView.contentOffset.x <= 0) {
scrollView.contentOffset = CGPointMake(UIScreen_width * _imageArray.count, 0); // 這里的4敌厘,是整個(gè)Image數(shù)組的個(gè)數(shù)。
}
// if (page == 0) {
// [scrollView setContentOffset:CGPointMake(UIScreen_width * _imageArray.count, 0)];
//
// }else if (page == _imageArray.count + 1){
// // 如果是第最后一頁就跳轉(zhuǎn)到數(shù)組第一個(gè)元素的地點(diǎn)
// [scrollView setContentOffset:CGPointMake(UIScreen_width, 0)];
// }
}
}
-(void)dealloc{
[_imageArray removeAllObjects];
[_ADScroll removeFromSuperview];
[_timer invalidate];
_timer = nil;
}
上邊就是一整個(gè)完整的Demo朽合,不過是最簡單的循環(huán)俱两。
如果想要看一些其他的可以看這里——UIScrollView(循環(huán)滾動(dòng)圖片)
以及——iOS_UIScrollView實(shí)現(xiàn)無限滾動(dòng),思路與代碼