效果:
思路:
- 先创建一个UIScrollView,然后根据图片的数量来设置UIScrollView的contentSize,通过block来回调图片的点击事件。
- 定时器每秒钟调用一次来实现自动滚动。
用法:
NSArray *imageUrlArray = @[@"http://pic24.nipic.com/20121003/10754047_140022530392_2.jpg", @"http://img15.3lian.com/2015/a1/13/d/17.jpg", @"http://pic36.nipic.com/20131022/7786988_135813187182_2.jpg"];
HWAutoScrollView *view = [[HWAutoScrollView alloc] initWithImageUrlArray:imageUrlArray placeHolder:@"loadingImage" imageOnClickCallBack:^(int imageIndex){
NSLog(@"-->> RemoteImageIndex:%d", imageIndex);
}];
[self.view addSubview:view];
__weak typeof(self) weakSelf = self;
[view makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(weakSelf.view);
make.top.equalTo(weakSelf.remoteLabel.bottom).offset(5);
make.height.equalTo(@(150));
}];
NSArray *imageNameArray = @[@"0001_meitu", @"0002_meitu", @"0003_meitu"];
HWAutoScrollView *view = [[HWAutoScrollView alloc] initWithImageNameArray:imageNameArray imageOnClickCallBack:^(int imageIndex){
NSLog(@"-->> LocalImageIndex:%d", imageIndex);
}];
[self.view addSubview:view];
__weak typeof(self) weakSelf = self;
[view makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(weakSelf.view);
make.top.equalTo(weakSelf.localLabel.bottom).offset(5);
make.height.equalTo(@(150));
}];
安装:
- 下载此Demo之后,拖动HWAutoScrollView文件夹到项目中,导入HWAutoScrollView.h
- 此工具类依赖Masonry,使用前请确保已导入或安装Masonry。
Demo链接:https://github.com/hw20101101/HWAutoScrollViewDemo