美文网首页
iOS 如何实现文字上下轮播

iOS 如何实现文字上下轮播

作者: 当阳桥 | 来源:发表于2017-06-13 15:37 被阅读125次

    下面是大致实现的模版、具体高度根据实际情况确定

    #import <UIKit/UIKit.h>
    
    
    @protocol ZYTextPageScrollViewDelegate <UIScrollViewDelegate>
    
    - (void)didSelectTextFromIndex:(NSInteger)index;
    
    @end
    
    @interface ZYTextPageScrollView : UIScrollView
    
    @property (nonatomic,strong) NSArray *textArray;
    
    @property (nonatomic,weak) id<ZYTextPageScrollViewDelegate>delegate;
    @end
    
    #import "ZYTextPageScrollView.h"
    @interface ZYTextPageScrollView()
    @property (nonatomic,strong) NSTimer *timer;
    @property (nonatomic,assign) NSInteger count;
    @property (nonatomic,strong) UILabel * topLabel;
    @property (nonatomic,strong) UILabel * bottomLabel;
    @property (nonatomic,strong) UIView * coverView;
    @end
    @implementation ZYTextPageScrollView
    #define contentW [[UIScreen mainScreen]bounds].size.width
    
    @dynamic delegate;
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            [self addSubViews];
        }
        return self;
    }
    - (NSArray *)textArray {
        if (_textArray.count==0) {
            _textArray = @[
                           @"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                           @"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
                           @"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
                           @"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
                           @"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
                           ];
        }
        return _textArray;
    }
    - (void)addSubViews {
        self.contentSize = CGSizeMake(contentW, 100*2);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.scrollEnabled = NO;
        _count = 0;
        
        
        _coverView = [[UIView alloc] init];
        _coverView.frame = CGRectMake(0, 0, contentW, 200);
        [_coverView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didselectIndex)]];
        _coverView.backgroundColor = [UIColor orangeColor];
        _coverView.alpha = 0.4;
        [self addSubview:_coverView];
        for (int i = 0; i<2; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.numberOfLines = 0;
            if (i==0) _topLabel = label;
            if (i==1) _bottomLabel = label;
            label.frame = CGRectMake(0, i*100, contentW, 100);
            [self addSubview:label];
        }
        
        _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(updateText) userInfo:nil repeats:YES];
        
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        
        [self setLabelText];
    }
    
    - (void)updateText {
        
        [self setLabelText];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.contentOffset = CGPointMake(0, 100);
        }completion:^(BOOL finished) {
            [self setLabelText];
            _count++;
        }];
    
    }
    
    - (void)setLabelText {
        
        NSInteger idx = _count%self.textArray.count;
        NSInteger idx2 = (_count+1)%self.textArray.count;
        
        if (self.contentOffset.y==100) {
            _topLabel.text = _bottomLabel.text;
            self.contentOffset = CGPointMake(0, 0);// 每次滚动之后马上重置位置
        }else{
            _topLabel.text = self.textArray[idx];
            _bottomLabel.text = self.textArray[idx2];
        }
    }
    - (void)didselectIndex {
    
       NSInteger idx = _count%self.textArray.count;
        if ([self.delegate respondsToSelector:@selector(didSelectTextFromIndex:)]) {
            [self.delegate didSelectTextFromIndex:idx];
        }
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS 如何实现文字上下轮播

          本文链接:https://www.haomeiwen.com/subject/iewdqxtx.html