轮播器

来源:互联网 发布:sqlserver清空数据库 编辑:程序博客网 时间:2024/06/02 20:46

无限轮播器,可在LJBHeadScrollView.m中自定义修改为SDWebImage加载方式,计时器和分页器可选设置。

1.

@protocol LJBHeadScrollViewDelegate <NSObject>


@optional

- (void)LJBHeadScrollView:(UIView *)view index:(NSInteger)index;


@end


@interface LJBHeadScrollView : UIView


/** 是否显示UIPageControl */

@property(assign)BOOL showPage;


/** 是否启用计时器 */

@property(assign)BOOL useTimer;


/** 计时器时间 */

@property(assign)NSTimeInterval times;


/** 图片集合 */

@property(nonatomic,strong)NSArray *imageLists;


@property(assign)id<LJBHeadScrollViewDelegate>delegate;

2.

@interface LJBHeadScrollView ()<UIScrollViewDelegate>


@property(nonatomic,weak)UIScrollView *scrollView;

@property(nonatomic,weak)UIPageControl *page;

@property(nonatomic,strong)NSTimer *timer;

@end


@implementation LJBHeadScrollView


- (void)setUpVc {

    if (self.scrollView) {

        [self.scrollViewremoveFromSuperview];

    }

    CGFloat padding =10;

    UIScrollView *scrollView = [[UIScrollViewalloc] init];

    self.scrollView = scrollView;

    scrollView.frame =CGRectMake(0,0, self.frame.size.width,self.frame.size.height);

    scrollView.pagingEnabled =YES;

    scrollView.showsHorizontalScrollIndicator =NO;

    scrollView.showsVerticalScrollIndicator =NO;

    scrollView.delegate =self;

    [selfaddSubview:scrollView];

    if (self.page) {

        [self.pageremoveFromSuperview];

    }

    if (self.showPage) {

        UIPageControl *page = [[UIPageControlalloc] init];

        self.page = page;

        page.numberOfPages =self.imageLists.count;

        page.currentPage =0;

        CGFloat pageWidth = padding * page.numberOfPages;

        CGFloat pageHeight = padding;

        page.frame =CGRectMake(self.frame.size.width * 0.5 - pageWidth * 0.5, self.frame.size.height - pageHeight - 2 * padding, pageWidth, pageHeight);

        page.pageIndicatorTintColor = [UIColorlightGrayColor];

        page.currentPageIndicatorTintColor = [UIColororangeColor];

        [selfaddSubview:page];

    }

}


- (void)setImageLists:(NSArray *)imageLists {

    _imageLists = imageLists;

    [selfsetUpVc];

    

    NSInteger count = imageLists.count <2?imageLists.count:(imageLists.count +2);

    for (int i =0; i < count; i++) {

        UIImageView *imageView = [[UIImageViewalloc] init];

        NSString *image =nil;

        if (i ==0) {

            image = [imageLists lastObject];

        } elseif (i == count - 1) {

            image = [imageLists firstObject];

        } else {

            image = imageLists[i - 1];

        }

        imageView.image = [UIImageimageNamed:image];

        imageView.frame =CGRectMake(i *self.scrollView.frame.size.width,0, self.scrollView.frame.size.width,self.scrollView.frame.size.height);

        imageView.tag = i -1;

        imageView.contentMode =UIViewContentModeScaleAspectFit;

        imageView.userInteractionEnabled =YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapImage:)];

        [imageView addGestureRecognizer:tap];

        [self.scrollViewaddSubview:imageView];

    }

    self.scrollView.contentSize =CGSizeMake(count * self.scrollView.frame.size.width,0);

    self.scrollView.contentOffset =CGPointMake(count < 2?0:self.scrollView.frame.size.width,0);

    

    [selfcreateTimer];

}


- (void)createTimer {

    [self.timerinvalidate];

    self.timer =nil;

    if (self.useTimer &&self.times) {

        self.timer = [NSTimerscheduledTimerWithTimeInterval:self.timestarget:selfselector:@selector(changeImage)userInfo:nilrepeats:YES];

        [[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSDefaultRunLoopMode];

    }

}


- (void)changeImage {

    NSInteger count =self.imageLists.count <2?self.imageLists.count:(self.imageLists.count + 2);

    if (count >=2) {

        CGPoint point =self.scrollView.contentOffset;

        if (point.x == (count -1) * self.scrollView.frame.size.width

            || point.x == (count -2) * self.scrollView.frame.size.width) {

            point.x =self.scrollView.frame.size.width;

            self.scrollView.contentOffset = point;


        } else {

            point.x +=self.scrollView.frame.size.width;

            [UIViewanimateWithDuration:0.5animations:^{

                self.scrollView.contentOffset = point;

            }];


        }

    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    [selfcreateTimer];

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGPoint point = scrollView.contentOffset;

    NSInteger count =self.imageLists.count <2?self.imageLists.count:(self.imageLists.count + 2);

    if (count >=2) {

        if (point.x >= scrollView.frame.size.width * (count - 1)) {

            scrollView.contentOffset =CGPointMake(scrollView.frame.size.width,0);

        }

        if (point.x <=0) {

            scrollView.contentOffset =CGPointMake(scrollView.frame.size.width * (count - 2), 0);

        }

        point = scrollView.contentOffset;

        NSInteger currentPage = (point.x +self.scrollView.frame.size.width * 0.5) / self.scrollView.frame.size.width;


        self.page.currentPage = currentPage -1;

        

    }

}


- (void)tapImage:(UITapGestureRecognizer *)tap {

    NSInteger tag = tap.view.tag;

    if ([self.delegaterespondsToSelector:@selector(LJBHeadScrollView:index:)]) {

        [self.delegateLJBHeadScrollView:selfindex:tag];

    }

}

0 0
原创粉丝点击