// // 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() @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