// // KXMainVerticleCycleView.m // CAISHEN // // Created by kuxuan on 2017/8/24. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXMainVerticleCycleView.h" @interface KXMainVerticleCycleView () { UIImageView *imageView; UIView *lineView; } @property (nonatomic,weak)UIScrollView *scrollView; @property (nonatomic,weak)NSTimer *scrollTimer; @end @implementation KXMainVerticleCycleView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code [self createView]; } return self; } -(void)createView { if (self.scrollView) { [self.scrollView removeFromSuperview]; } imageView=[[UIImageView alloc]initWithFrame:CGRectMake(Fitsize(9),13*SCREEN_MUTI,36*SCREEN_MUTI, 25*SCREEN_MUTI)]; imageView.userInteractionEnabled=YES; imageView.image=[UIImage imageNamed:@"main_notice"]; [self addSubview:imageView]; //创建轮播 UIScrollView *sc=[[UIScrollView alloc]initWithFrame:CGRectMake(Fitsize(53), 0, SCREEN_WIDTH-42, SCREEN_MUTI*30)]; self.scrollView=sc; self.scrollView.bounces = NO; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.alwaysBounceVertical=YES; self.scrollView.alwaysBounceHorizontal=NO; self.scrollView.delegate = self; self.scrollView.scrollEnabled=YES; self.scrollView.tag=6667; [self addSubview:self.scrollView]; } -(void)setImageName:(NSString *)imageName { _imageName = imageName; imageView.image=[UIImage imageNamed:imageName]; } -(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.scrollView.contentOffset.y/(30*SCREEN_MUTI)+0.5; index=index+1; [UIView animateWithDuration:1 animations:^{ self.scrollView.contentOffset=CGPointMake(0, 30*SCREEN_MUTI*index); }]; if (index==self.noticeArray.count+1) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)) animated:NO]; }else if(index==0) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO]; } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView.tag==6667) { int pageNum=self.scrollView.contentOffset.y/self.frame.size.height; if (pageNum==self.noticeArray.count+1) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(scrollView.frame)) animated:NO]; }else if(pageNum==0) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO]; } } } - (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==6667) { int pageNum=scrollView.contentOffset.y/CGRectGetHeight(scrollView.frame); if (pageNum==self.noticeArray.count+1) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(scrollView.frame)) animated:NO]; } else if(pageNum==0) { [self.scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.scrollView.frame)*self.noticeArray.count) animated:NO]; } } } -(void)setNoticeArray:(NSArray *)noticeArray { _noticeArray=noticeArray; if (noticeArray.count == 0){ return; } for (int i=0; i