美文网首页
iCarousel用法

iCarousel用法

作者: 守护地中海的花 | 来源:发表于2019-07-15 14:32 被阅读0次
#import "iCarousel.h"
#import "CustomPageControl.h"

@property(nonatomic,assign)BOOL viewDidAppear;

<iCarouselDelegate,iCarouselDataSource>
@property(nonatomic,strong)iCarousel *bannerIC;
@property(nonatomic,strong)CustomPageControl *bannerPG;
@property(nonatomic,weak)NSTimer *bannerTimer;
//数据
    [self.bannerIC reloadData];
//定时器
    dispatch_async(dispatch_get_main_queue(), ^{
        self.viewDidAppear = YES;
    });
- (void)setViewDidAppear:(BOOL)viewDidAppear
{
    _viewDidAppear = viewDidAppear;
    if (self.bannerModel.data.banner.count < 2) {
        return;
    }
    if (viewDidAppear) {
        self.bannerPG.numberOfPages = self.bannerModel.data.banner.count;
        [self.bannerTimer invalidate];
        self.bannerTimer = nil;
        self.bannerTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(rollBannerIC) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:self.bannerTimer forMode:NSRunLoopCommonModes];
    } else {
        [self.bannerTimer invalidate];
        self.bannerTimer = nil;
    }
}
- (void)rollBannerIC
{
    [self.bannerIC scrollToItemAtIndex:(self.bannerIC.currentItemIndex + 1) animated:YES];
}

#pragma mark - iCarousel代理
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.bannerModel.data.banner.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    if (!view) {
        view = [[UIView alloc]initWithFrame:carousel.frame];
        view.backgroundColor = [UIColor whiteColor];
        
        UIImageView *bIV = [[UIImageView alloc]initWithFrame:CGRectMake(12*ADAPTER_WIDTH, 0, carousel.width - 24*ADAPTER_WIDTH, carousel.height)];
        [view addSubview:bIV];
        bIV.contentMode = UIViewContentModeScaleAspectFill;
        bIV.layer.cornerRadius = 8*ADAPTER_WIDTH;
        bIV.layer.masksToBounds = YES;
        bIV.tag = 100;
    }
    UIImageView *bIV = [view viewWithTag:100];
    [bIV sd_setImageWithURL:self.bannerModel.data.banner[index].url.wppURL placeholderImage:[UIImage getPNGimageInBundleWithName:kPlaceHoldBanner]];
    return view;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    if (option == iCarouselOptionWrap)
    {
        return YES;
    }
    return value;
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    self.bannerPG.currentPage = carousel.currentItemIndex;
}
- (void)carouselWillBeginDragging:(iCarousel *)carousel
{
    self.viewDidAppear = NO;
}
- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate
{
    self.viewDidAppear = YES;
}
#pragma mark - lazy懒加载
- (iCarousel *)bannerIC
{
    if (!_bannerIC) {
        iCarousel *ic = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, self.width, 120*ADAPTER_WIDTH)];
        [self addSubview:ic];
        ic.scrollSpeed = 1;
        ic.type = 0;
        ic.pagingEnabled = YES;
        ic.delegate = self;
        ic.dataSource = self;
        ic.backgroundColor = [UIColor whiteColor];
        ic.clipsToBounds = YES;
        _bannerIC = ic;
    }
    return _bannerIC;
}
- (CustomPageControl *)bannerPG
{
    if (!_bannerPG) {
        CustomPageControl *pg = [[CustomPageControl alloc]initWithFrame:CGRectMake(0, self.bannerIC.height - 20, self.bannerIC.width, 8)];
        [self.bannerIC addSubview:pg];
        _bannerPG = pg;
    }
    return _bannerPG;
}

折叠效果

value * 0.9//滑动存在折叠
value * 1.1 不存在

相关文章

网友评论

      本文标题:iCarousel用法

      本文链接:https://www.haomeiwen.com/subject/sgjnkctx.html