iOS 如何实现文字上下轮播

2017-06-13  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读