KWMBannerView.m 7.32 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
//
//  KWMBannerView.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/5/31.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMBannerView.h"

@implementation KWMBannerView{
    iCarousel *carousel;
    NSTimer *timer;
    BOOL isFirstLoad;
    BOOL isScroll;
    
}

-(void)addTimer{
    [self removeTimer];
    isFirstLoad = YES;
    timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(doTimer) userInfo:nil repeats:YES];
    [timer setFireDate:[NSDate distantPast]];//开启
}

-(void)removeTimer{
    //结束
    if([timer isValid]){
        [timer invalidate];
        timer = nil;
    }
}

-(void)doTimer{
    NSInteger pageCount = self.delegate?[self.delegate numberOfPagesForBanner:self]:0;
    if(pageCount > 0 && !isScroll){
        if(isFirstLoad){ //避免出现第一次显示就是第二页的情况
            isFirstLoad = NO;
            return;
        }
        NSInteger currentIndex = self.pageIndex;
        currentIndex ++;
        [carousel scrollToItemAtIndex:currentIndex animated:YES];
        [self updateUIOnPageChange];
    }
}

-(instancetype)init{
    self = [super init];
    if(self){
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initBannerView:frame];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initBannerView:self.frame];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void)setDelegate:(id<KWMBannerViewDelegate>)delegate{
    _delegate = delegate;
    if(delegate){
        [self reloadData];
    }
}

-(void)reloadData{
    if(carousel){
        [carousel reloadData];
    }
    if(self.foregroundView && self.foregroundView.tag == 1002){
        KWMPageControl *pageControl = (KWMPageControl *)self.foregroundView;
houweibin committed
88
        pageControl.numberOfPages = [self numberOfItemsInCarousel:carousel];
houweibin committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        //pageControl圆点右对齐
        NSInteger count = self.delegate?[self.delegate numberOfPagesForBanner:self]:0;
        CGSize pageControlSize = [pageControl sizeForNumberOfPages:count];
        CGFloat paddingLeft = (pageControl.bounds.size.width - pageControlSize.width) / 2 - 15;
        [pageControl setBounds:CGRectMake(-paddingLeft, pageControl.bounds.origin.y,
                                          pageControl.bounds.size.width, pageControl.bounds.size.height)];
    }
}

-(void)initBannerView:(CGRect)frame{
    carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    carousel.delegate = self;
    carousel.dataSource = self;
    carousel.decelerationRate = 0.5;
    carousel.type = iCarouselTypeLinear;
    carousel.clipsToBounds = YES;
    [self addSubview:carousel];
    
    UIImageView *noDataImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    noDataImageView.contentMode = UIViewContentModeCenter;
    UIImage *noDataImage = [UIImage imageNamed:@"ic_logo"];
    noDataImageView.image = noDataImage;
    noDataImageView.tag = 1001;//用于判断是否默认背景view
    self.backgroundView = noDataImageView;
    
    CGRect pageControlFrame = CGRectMake(0, frame.size.height - 20, frame.size.width, 20);
    UIImage *defaultImage = [UIImage imageNamed:@"ic_page_1"];
    UIImage *currentImage = [UIImage imageNamed:@"ic_page_2"];
    KWMPageControl *pageControl = [[KWMPageControl alloc]initWithFrame:pageControlFrame currentImage:currentImage andDefaultImage:defaultImage];
    pageControl.numberOfPages = 4;
    pageControl.currentPage = 0;
    pageControl.tag = 1002;//用于判断是否默认前景view
    self.foregroundView = pageControl;
}

-(NSInteger)pageIndex{
    if(carousel){
        return carousel.currentItemIndex;
    }
    return 0;
}

-(void)setBackgroundView:(UIView *)backgroundView{
    _backgroundView = backgroundView;
    UIView *oldBackgroundView  = [self viewWithTag:101];
    if(oldBackgroundView){
        [oldBackgroundView removeFromSuperview];
    }
    oldBackgroundView = [self viewWithTag:1001];
    if(oldBackgroundView){
        [oldBackgroundView removeFromSuperview];
    }
    if(backgroundView){
        backgroundView.tag = 101;
        [self addSubview:backgroundView];
        [self sendSubviewToBack:backgroundView];
    }
}

-(void)setForegroundView:(UIView *)foregroundView{
    _foregroundView = foregroundView;
    UIView *oldForegroundView  = [self viewWithTag:102];
    if(oldForegroundView){
        [oldForegroundView removeFromSuperview];
    }
    oldForegroundView = [self viewWithTag:1002];
    if(oldForegroundView){
        [oldForegroundView removeFromSuperview];
    }
    if(foregroundView){
        //设置tag前先判断是否默认foregroundView,以免覆盖了tag
        foregroundView.tag = foregroundView.tag == 1002?1002:102;
        [self addSubview:foregroundView];
        [self bringSubviewToFront:foregroundView];
    }
}

-(void)updateUIOnPageChange{
    
    if(self.foregroundView && self.foregroundView.tag == 1002){
        KWMPageControl *pageControl = (KWMPageControl *)self.foregroundView;
        pageControl.currentPage = self.pageIndex;
    }
    
    if(self.delegate){
        if([self.delegate respondsToSelector:@selector(bannerView:onPageIndexChange:)]){
            [self.delegate bannerView:self onPageIndexChange:self.pageIndex];
        }
        if([self.delegate respondsToSelector:@selector(bannerView:backgroundView:updateBackgroundViewAtIndex:)]
           && self.backgroundView){
            [self.delegate bannerView:self backgroundView:self.backgroundView updateBackgroundViewAtIndex:self.pageIndex];
        }
        if([self.delegate respondsToSelector:@selector(bannerView:foregroundView:updateForegroundViewAtIndex:)]
           && self.foregroundView){
            [self.delegate bannerView:self foregroundView:self.backgroundView updateForegroundViewAtIndex:self.pageIndex];
        }
    }
}

#pragma mark iCarousel methods
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    if(self.delegate){
        return [self.delegate numberOfPagesForBanner:self];
    }
    return 0;
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    return 3;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    if(self.delegate){
        return [self.delegate bannerView:self reusingView:view pageAtIndex:index];
    }
    return nil;
}


-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
    if(self.delegate){
        [self.delegate bannerView:self onClickPage:index];
    }
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate{
    [self updateUIOnPageChange];
}

- (void)carouselDidEndDecelerating:(iCarousel *)carousel{
    [self updateUIOnPageChange];
}


- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return self.frame.size.width;
}

-(void)carouselDidEndScrollingAnimation:(iCarousel *)carousel{
    isScroll = NO;
}

-(void)carouselDidScroll:(iCarousel *)carousel{
    isScroll = YES;
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            return YES;
        }
        default:
        {
            return value;
        }
    }
}






@end