123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- //
- // KXMainHorizontalCycleView.m
- // QBCS
- //
- // Created by kuxuan on 2017/6/6.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "KXMainHorizontalCycleView.h"
- #import "KXCycleModel.h"
- #import "KXCustomPageControl.h"
- @interface KXMainHorizontalCycleView()<UIScrollViewDelegate>
- @property (nonatomic,strong)UIScrollView *topScrollView;
- @property (nonatomic,weak)NSTimer *scrollTimer;
- @property (nonatomic,strong)KXCustomPageControl *pageControl;
- @end
- @implementation KXMainHorizontalCycleView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- [self createView];
- }
- return self;
- }
- -(void)createView
- {
- if (self.topScrollView) {
- [self.topScrollView removeFromSuperview];
- }
- //创建轮播
- UIScrollView *sc=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width,120*SCREEN_MUTI)];
- self.topScrollView=sc;
- self.topScrollView.bounces = NO;
- self.topScrollView.pagingEnabled = YES;
- self.topScrollView.showsHorizontalScrollIndicator = NO;
- self.topScrollView.showsVerticalScrollIndicator = NO;
- self.topScrollView.delegate = self;
- self.topScrollView.scrollEnabled=YES;
- self.topScrollView.tag=6666;
- [self addSubview:self.topScrollView];
-
- }
- -(void)setupTimer
- {
- NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
- self.scrollTimer = timer;
- [[NSRunLoop mainRunLoop] addTimer:self.scrollTimer forMode:NSRunLoopCommonModes];
- }
- -(void)automaticScroll
- {
- NSInteger index=self.topScrollView.contentOffset.x/SCREEN_WIDTH;
- index=index+1;
- [UIView animateWithDuration:1 animations:^{
- self.topScrollView.contentOffset=CGPointMake(SCREEN_WIDTH*index, 0);
- }];
- self.pageControl.indexNumWithSlide=index;
- if (index==self.scrollArray.count+1) {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.topScrollView.frame), 0) animated:NO];
- self.pageControl.indexNumWithSlide=0;
- }else if(index==0)
- {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(self.topScrollView.frame)*self.scrollArray.count, 0) animated:NO];
- self.pageControl.indexNumWithSlide=self.scrollArray.count;
-
- }else{
- self.pageControl.indexNumWithSlide=index-1;
- }
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- if (scrollView.tag==6666) {
- int pageNum=self.topScrollView.contentOffset.x/SCREEN_WIDTH;
- self.pageControl.indexNumWithSlide=pageNum;
- if (pageNum==self.scrollArray.count+1) {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
- self.pageControl.indexNumWithSlide=0;
-
- }else if(pageNum==0)
- {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame)*self.scrollArray.count, 0) animated:NO];
- self.pageControl.indexNumWithSlide=self.scrollArray.count;
-
- }
- else
- {
- self.pageControl.indexNumWithSlide=pageNum-1;
-
- }
- }
-
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
-
- [self.scrollTimer invalidate];
- self.scrollTimer = nil;
-
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
-
- [self setupTimer];
-
- }
- - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
- {
- if (scrollView.tag==6666) {
- int pageNum=scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);
- self.pageControl.indexNumWithSlide=pageNum;
-
-
- if (pageNum==self.scrollArray.count+1) {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
- self.pageControl.indexNumWithSlide=0;
- }
- else if(pageNum==0)
- {
- [self.topScrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame)*self.scrollArray.count, 0) animated:NO];
- self.pageControl.indexNumWithSlide=self.scrollArray.count;
- }
- else
- {
- self.pageControl.indexNumWithSlide=pageNum-1;
- }
- }
-
- }
- -(void)setScrollArray:(NSArray *)scrollArray
- {
- _scrollArray=scrollArray;
-
- if (_pageControl) {
- [_pageControl removeFromSuperview];
- }
- _pageControl = [KXCustomPageControl cusPageControlWithView:CGRectMake(SCREEN_WIDTH-100,self.frame.size.height-20, 100, 20) pageNum:scrollArray.count currentPageIndex:0 currentShowImage:[UIImage imageNamed:@"home_pageControl_select"] pageIndicatorShowImage:[UIImage imageNamed:@"home_pageControl_normal"]];
- [self addSubview:_pageControl];
- for (int i=0; i<self.scrollArray.count+2; i++) {
- UIImageView *imageView=[[UIImageView alloc]init];
- imageView.frame=CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, 120*SCREEN_MUTI);
- imageView.userInteractionEnabled=YES;
- if (i==0) {
- KXCycleModel *model=scrollArray[scrollArray.count-1];
- [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- }else if (i==self.scrollArray.count+1){
- KXCycleModel *model=scrollArray[0];
- [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- }else{
- KXCycleModel *model=scrollArray[i-1];
- [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- imageView.tag=2345+i;
- }
-
- UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
- [imageView addGestureRecognizer:tap];
- [self.topScrollView addSubview:imageView];
- }
- self.topScrollView.contentSize=CGSizeMake((self.scrollArray.count+2)*SCREEN_WIDTH, 0);
-
- self.topScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
-
- // self.pageControl.pageIndicatorTintColor=[UIColor colorWithWhite:1 alpha:0.4];
- // self.pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
- [self setupTimer];
- }
- //- (UIPageControl *)pageControl{
- // if (!_pageControl) {
- // _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100,self.frame.size.height-20, 100, 20)];
- //// [_pageControl setValue:[UIImage imageNamed:@"home_pageControl_normal"] forKey:@"_pageImage"];
- //// [_pageControl setValue:[UIImage imageNamed:@"home_pageControl_select"] forKey:@"_currentPageImage"];
- // _pageControl.pageIndicatorTintColor=[UIColor colorWithWhite:1 alpha:0.4];
- // _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
- // }
- // return _pageControl;
- //}
- -(void)dealloc
- {
- self.topScrollView.delegate=nil;
- }
- -(void)tapAction:(UITapGestureRecognizer *)tap
- {
- [_delegate clickScrollViewItemWithIndex:tap.view.tag-2346];
- }
- @end
|