下载封装好的代码
地址:https://pan.baidu.com/s/1hstjJH6
//①先导入头文件
#import "MBDICycleScrollView.h"
//②初始化滚动视图和图片数组
@interface LUNBOViewController (){
MBDICycleScrollView * ScrolView;
NSMutableArray * imgData;
NSMutableArray * imgViews;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"无限滚动视图";
//初始化滚动视图 设置位置及大小 P1 frame P2跳转一次的时间
ScrolView = [[MBDICycleScrollView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300) animationDuration:2];
//初始化imgView 数组
imgViews = [[NSMutableArray alloc]init];
//将滚动视图添加到视图上
[self.view addSubview:ScrolView];
//将图片添加到可变数组里
imgData = [NSMutableArray array];
for (NSInteger i = 1; i<7; i++) {
[imgData addObject:[NSString stringWithFormat:@"%ld.jpg",(long)i]];
}
//调用updateScrolView 方法
[self updateScrolView];
}
-(void)updateScrolView{
//向图片数组里添加图片,并将图片添加到视图上
imgViews=[NSMutableArray array];
[imgViews removeAllObjects];
for (int i =0; i <imgData.count; i++) {
UIView *photoV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
//设置用户交互性(View 能被点击)
[photoV setUserInteractionEnabled:YES];
UIImageView *imgv = [[UIImageView alloc]initWithFrame:photoV.bounds];
[imgv setUserInteractionEnabled:YES];
[imgv setContentMode:UIViewContentModeScaleAspectFill];
//网络请求数组
// [imgv sd_setImageWithURL:[NSURL URLWithString:_CycleDataList[i][@"img"]] placeholderImage:IMAGEDEFAULT completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// } ];
[imgv setImage:[UIImage imageNamed:imgData[i]]];
[photoV addSubview:imgv];
[imgViews addObject:photoV];
}
__block __weak LUNBOViewController *weakSelf = self;
//调用 setArrayView 方法
ScrolView.arrayView = imgViews;
//赋值 block 代码块
ScrolView.TapActionBlock = ^(NSInteger pageIndex){
NSLog(@"tap:%ld",(long)pageIndex);
//自身调用方法
[weakSelf tapClick:pageIndex];
};
}
-(void)tapClick:(NSInteger)index{
NSLog(@"%ld",(long)index);
}