123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //
- // KXTeachCycleView.m
- // CAISHEN
- //
- // Created by kuxuan on 2017/11/1.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "KXTeachCycleView.h"
- @interface KXTeachCycleView()<UIScrollViewDelegate>
- @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<self.cycleArray.count; i++) {
- UIImageView *imageView=[[UIImageView alloc]init];
- imageView.frame=CGRectMake(i*140, 0,135, 70);
- imageView.userInteractionEnabled=YES;
- [imageView setExclusiveTouch:YES];
- NSString *imageString=cycleArray[i];
- [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- imageView.tag=2345+i;
- // if (i==0) {
- // NSString *imageString=cycleArray[cycleArray.count-1];
- // [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- // }else if (i==self.cycleArray.count+1){
- // NSString *imageString=cycleArray[0];
- // [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"placeholder_rectangle"] options:SDWebImageRefreshCached];
- // }else{
- // NSString *imageString=cycleArray[i-1];
- // [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] 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.cycleArray.count)*140, 0);
-
- self.topScrollView.contentOffset=CGPointMake(0, 0);
-
- // [self setupTimer];
- }
- -(void)dealloc
- {
- self.topScrollView.delegate=nil;
- }
- -(void)tapAction:(UITapGestureRecognizer *)tap
- {
- [_delegate clickScrollViewItemWithIndex:tap.view.tag-2345];
- }
- @end
|