123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- //
- // KXMainVerticleCycleView.m
- // CAISHEN
- //
- // Created by kuxuan on 2017/8/24.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "KXMainVerticleCycleView.h"
- @interface KXMainVerticleCycleView ()<UIScrollViewDelegate>
- {
- 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<self.noticeArray.count+2; i++) {
-
- UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,30*SCREEN_MUTI*i, SCREEN_WIDTH-42, 46*SCREEN_MUTI)];
-
- titleLabel.textColor = [UIColor KXColorWithHex:0x666666];
- titleLabel.tag=i+2345;
- titleLabel.font = FONT_SYS(14);
- [self.scrollView addSubview:titleLabel];
- if (i==0) {
- NSString *aString=noticeArray[self.noticeArray.count-1];
- NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
- [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
-
- titleLabel.attributedText = attrStr;
- }else if (i==self.noticeArray.count+1){
- NSString *aString=noticeArray[0];
- NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
- [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
- titleLabel.attributedText = attrStr;
- }else{
- NSString *aString=noticeArray[i-1];
- NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[aString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:FONT_SYS(20*SCREEN_MUTI)} documentAttributes:nil error:nil];
- [attrStr addAttribute:NSFontAttributeName value:FONT_SYS(14) range:NSMakeRange(0, [attrStr length]-1)];
- titleLabel.attributedText = attrStr;
- }
- titleLabel.userInteractionEnabled=YES;
- UITapGestureRecognizer *titleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleTapAction:)];
- [titleLabel addGestureRecognizer:titleTap];
- }
- self.scrollView.contentSize=CGSizeMake(SCREEN_WIDTH-42, (noticeArray.count+2)*(30*SCREEN_MUTI));
-
- self.scrollView.contentOffset=CGPointMake(0, 30.0*SCREEN_MUTI);
-
- if (noticeArray.count!=1) {
- [self setupTimer];
- }
-
- }
- -(void)dealloc
- {
- self.scrollView.delegate=nil;
- }
- -(void)titleTapAction:(UITapGestureRecognizer *)tap
- {
- [_delegate clickmiddleViewWithIndex:tap.view.tag-2346];
- }
- -(void)setIsHuaJiangHu:(BOOL)isHuaJiangHu {
- _isHuaJiangHu = isHuaJiangHu;
- if (isHuaJiangHu) {
- lineView = [[UIView alloc] init];
- [self addSubview:lineView];
- [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(imageView.mas_top).mas_offset(5);
- make.bottom.mas_equalTo(imageView.mas_bottom).mas_offset(-8);
- make.left.mas_equalTo(imageView.mas_right).mas_offset(Fitsize(5));
- make.width.mas_equalTo(Fitsize(1));
- }];
- lineView.backgroundColor = [UIColor KXColorWithHex:0xFF5D49];
- }else {
- [lineView removeFromSuperview];
- lineView = nil;
- }
- }
- @end
|