开源库名称: JYCarousel
开源库当前版本: 0.0.2
开源库简介: 这是一个使用非常简单且自带下载和缓存的开放源代码轮播库,不会造成循环引用,不需要考虑定时器不能销毁(我都已经为您做了)。
pod 'JYCarousel', '~> 0.0.2'
使用三个 imageView 添加到 ScrollView,始终保持中间的 imageView 在可视界面里。当前的 imageView 滚动到下一个 imageView,然后把下一个 imageView 滚动到三个 imageView 的中心位置,在这个过程中三个 imageView 同时赋值,找到最中间的 imageView,把它设置为当前索引,滚动完成后设置为滚动中心位置。
比如三张图 A、B、C。要做的 scrollview 实际上是五张,大小顺序为 C、A、B、C、A。初始偏移量设置到第二张,监听 scrollview 滑动事件,判断偏移量。当偏移量在第一张时将偏移量修改到第四张,当偏移量在第五张时将偏移量调整到第二张。这样在循环时比较流畅,才能无缝无限循环滚动。
无缝循环轮播,处理得很好,不会显得生硬。
自带图片下载和缓存,不依赖任何第三方,引入即可使用,无需任何配置。
支持 block 方式和 delegate 方式,使用起来极为方便。
随时根据需要清除缓存。
采用磁盘缓存,不会占用 app 内存,释放 app 内存。
可以随时更新轮播数据,完美切换。
用户可自定义的属性很多,具体请参见配置文件 JYConfiguration。
JYCarousel
/**
block方式回调初始化
@param frame frame
@param configBlock 轮播属性配置
@param clickBlock 点击回调
@return carousel
*/
- (instancetype)initWithFrame:(CGRect)frame
configBlock:(CarouselConfigurationBlock)configBlock
clickBlock:(CarouselClickBlock)clickBlock;
/**
delegate方式回调初始化
@param frame frame
@param configBlock 轮播属性配置
@param target delegate
@return carousel
*/
- (instancetype)initWithFrame:(CGRect)frame
configBlock:(CarouselConfigurationBlock)configBlock
target:(id<JYCarouselDelegate>)target;
- (void)addCarouselView1{
//block方式创建
__weak typeof(self) weakSelf = self;
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithArray: @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"]];
if (!_carouselView1) {
_carouselView1= [[JYCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100) configBlock:^JYConfiguration *(JYConfiguration *carouselConfig) {
carouselConfig.pageContollType = RightPageControl;
carouselConfig.interValTime = 3;
return carouselConfig;
} clickBlock:^(NSInteger index) {
[weakSelf clickIndex:index];
}];
[self.view addSubview:_carouselView1];
}
//开始轮播
[_carouselView1 startCarouselWithArray:imageArray];
}
//遵循协议
@interface SubViewController ()<JYCarouselDelegate>
//创建
- (void)addCarouselView2{
NSMutableArray *imageArray2 = [[NSMutableArray alloc] initWithArray: @[@"http://p1.bqimg.com/524586/894925a41a745ba8.jpg",@"http://p1.bqimg.com/524586/edd59898ac21642f.jpg",@"http://p1.bqimg.com/524586/d277aa654cd60c3d.jpg",@"http://p1.bqimg.com/524586/a49b8d3e1b953f25.jpg",@"http://p1.bqimg.com/524586/972bff3b7a5fb7e1.jpg"]];
NSMutableArray *titleArray2 = [[NSMutableArray alloc] initWithArray: @[@"http://p1.bqimg.com/524586/894925a41a745ba8.jpg",@"http://p1.bqimg.com/524586/edd59898ac21642f.jpg",@"http://p1.bqimg.com/524586/d277aa654cd60c3d.jpg",@"http://p1.bqimg.com/524586/a49b8d3e1b953f25.jpg",@"http://p1.bqimg.com/524586/972bff3b7a5fb7e1.jpg"]];
if (!_carouselView2) {
_carouselView2 = [[JYCarousel alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, 100) configBlock:^JYConfiguration *(JYConfiguration *carouselConfig) {
carouselConfig.pageContollType = MiddlePageControl;
carouselConfig.pageTintColor = [UIColor whiteColor];
carouselConfig.currentPageTintColor = [UIColor redColor];
carouselConfig.placeholder = [UIImage imageNamed:@"default"];
carouselConfig.faileReloadTimes = 5;
carouselConfig.textAlignment = NSTextAlignmentLeft;
carouselConfig.pageContollType = LabelPageControl;
return carouselConfig;
} target:self];
[self.view addSubview:_carouselView2];
}
//开始轮播
[_carouselView2 startCarouselWithArray:imageArray2 titleArray:titleArray2];
}
//回调方法
- (void)carouselViewClick:(NSInteger)index{
NSLog(@"代理方式你点击图片索引index = %ld",index);
//清楚缓存数据 可以在app启动的时候清楚上一次轮播缓存,根据自己需要
[[JYImageCache sharedImageCache] jy_clearDiskCaches];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//添加以下方法即可
//每次启动时,检查是否超时,清除缓存(单位是小时这里是10分钟)
[[JYImageCache sharedImageCache] jy_clearDiskCachesWithTimeout:10.0/60];
}
//清除缓存数据 根据自己需要调用
[[JYImageCache sharedImageCache] jy_clearDiskCaches];
内存没有得到释放,造成内存泄漏。请注意使用。看看下面在 block 回调处,对 Self 是使用弱引用的,否则内存是无法释放的。
// 请使用weakSelf,不然内存得不到释放 __weak typeof(self) weakSelf = self; //图片数组(或者图片URL,图片URL字符串,图片UIImage对象) NSMutableArray *imageArray = [[NSMutableArray alloc] initWithArray: @[@1.jpg,@2.jpg,@3.jpg,@4.jpg]]; JYCarousel *carouselView = [[JYCarousel alloc] initWithFrame:CGRectMake(0, 64, ViewWidth(self.view), 100) configBlock:nil clickBlock:NSInteger index { //点击imageView回调方法 [weakSelf clickIndex:index]; }]; //开始轮播 [carouselView startCarouselWithArray:imageArray]; [self.view addSubview:carouselView];