// // KXTeachCycleView.m // CAISHEN // // Created by kuxuan on 2017/11/1. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXTeachCycleView.h" @interface KXTeachCycleView() @property (nonatomic,weak)UIScrollView *topScrollView; @property (nonatomic,weak)NSTimer *scrollTimer; @end @implementation KXTeachCycleView - (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, SCREEN_WIDTH - 15,70)]; 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/(140); index=index+1; [UIView animateWithDuration:1 animations:^{ self.topScrollView.contentOffset=CGPointMake((140)*index, 0); }]; if (index==self.cycleArray.count+1) { [self.topScrollView setContentOffset:CGPointMake(140, 0) animated:NO]; } else if (index==0) { [self.topScrollView setContentOffset:CGPointMake(140*self.cycleArray.count, 0) animated:NO]; } else { } } #pragma mark - UIScrollViewDelegate //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView //{ // if (scrollView.tag==6666) { // float pageNum=self.topScrollView.contentOffset.x/140; // int index = pageNum +0.5; // [UIView animateWithDuration:0.1 animations:^{ // self.topScrollView.contentOffset=CGPointMake((140)*index, 0); // // }]; // if (index==self.cycleArray.count+1) { // [self.topScrollView setContentOffset:CGPointMake(140, 0) animated:NO]; // // } // else if (index==0) { // [self.topScrollView setContentOffset:CGPointMake(140*self.cycleArray.count, 0) animated:NO]; // // // } // else { // } // } // //} // //- (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) { // float pageNum=self.topScrollView.contentOffset.x/280.0; // int index = pageNum +0.3; // [UIView animateWithDuration:0.1 animations:^{ // self.topScrollView.contentOffset=CGPointMake((280)*index, 0); // // }]; // if (index==self.cycleArray.count+1) { // [self.topScrollView setContentOffset:CGPointMake(280, 0) animated:NO]; // // }else if(index==0) // { // [self.topScrollView setContentOffset:CGPointMake(280*self.cycleArray.count, 0) animated:NO]; // // // }else{ // // } // } // //} - (void)setCycleArray:(NSArray *)cycleArray { _cycleArray=cycleArray; for (int i=0; i