下載封裝好的代碼
地址:https://pan.baidu.com/s/1hstjJH6
//①先導(dǎo)入頭文件
#import "MBDICycleScrollView.h"
//②初始化滾動(dòng)視圖和圖片數(shù)組
@interface LUNBOViewController (){
MBDICycleScrollView * ScrolView;
NSMutableArray * imgData;
NSMutableArray * imgViews;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"無(wú)限滾動(dòng)視圖";
//初始化滾動(dòng)視圖 設(shè)置位置及大小 P1 frame P2跳轉(zhuǎn)一次的時(shí)間
ScrolView = [[MBDICycleScrollView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300) animationDuration:2];
//初始化imgView 數(shù)組
imgViews = [[NSMutableArray alloc]init];
//將滾動(dòng)視圖添加到視圖上
[self.view addSubview:ScrolView];
//將圖片添加到可變數(shù)組里
imgData = [NSMutableArray array];
for (NSInteger i = 1; i<7; i++) {
[imgData addObject:[NSString stringWithFormat:@"%ld.jpg",(long)i]];
}
//調(diào)用updateScrolView 方法
[self updateScrolView];
}
-(void)updateScrolView{
//向圖片數(shù)組里添加圖片,并將圖片添加到視圖上
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)];
//設(shè)置用戶交互性(View 能被點(diǎn)擊)
[photoV setUserInteractionEnabled:YES];
UIImageView *imgv = [[UIImageView alloc]initWithFrame:photoV.bounds];
[imgv setUserInteractionEnabled:YES];
[imgv setContentMode:UIViewContentModeScaleAspectFill];
//網(wǎng)絡(luò)請(qǐng)求數(shù)組
//? ? ? ? ? ? ? ? [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;
//調(diào)用 setArrayView 方法
ScrolView.arrayView = imgViews;
//賦值 block 代碼塊
ScrolView.TapActionBlock = ^(NSInteger pageIndex){
NSLog(@"tap:%ld",(long)pageIndex);
//自身調(diào)用方法
[weakSelf tapClick:pageIndex];
};
}
-(void)tapClick:(NSInteger)index{
NSLog(@"%ld",(long)index);
}