#import <UIKit/UIKit.h> @interface ScrollUpDownView : UIView //设置 要现实的文字 @property(nonatomic, strong)NSArray *array; // 开启定时器 - (void)onOpenTimer; // 关闭定时器 - (void)onOffTimer; @end
#import "ScrollUpDownView.h" @interface ScrollUpDownView () @property(nonatomic, weak)UILabel *label1; @property(nonatomic, weak)UILabel *label2; @property(nonatomic, assign)NSInteger count; @property(nonatomic, strong)NSTimer *timer; @property(nonatomic, assign) CGFloat mainH; @property(nonatomic, assign) CGFloat mainW; @property(nonatomic, assign) CGFloat mainX; @property(nonatomic, assign) CGFloat mainY; @end @implementation ScrollUpDownView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initUIFrame:frame]; } return self; } - (void)setArray:(NSArray *)array { _array = array; } - (void)initUIFrame:(CGRect)rect { self.backgroundColor = [UIColor whiteColor]; _mainH = rect.size.height; _mainW = rect.size.width; _mainX = 0; _mainY = 0; // bgView UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(_mainX, _mainY, _mainW, _mainH)]; bgView.layer.masksToBounds = YES; bgView.backgroundColor = [UIColor yellowColor]; bgView.layer.masksToBounds = YES; bgView.layer.cornerRadius = CGRectGetHeight(bgView.frame) / 2; [self addSubview:bgView]; // 11111 UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(_mainX, _mainY, _mainW, _mainH)]; self.label1 = label1; self.label1.textAlignment = NSTextAlignmentCenter; self.label1.textColor = [UIColor whiteColor]; self.label1.font = [UIFont systemFontOfSize:12]; label1.backgroundColor = [UIColor redColor]; [bgView addSubview:label1]; // 22222 UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(_mainX, _mainH, _mainW, _mainH)]; self.label2 = label2; self.label2.textAlignment = NSTextAlignmentCenter; self.label2.textColor = [UIColor whiteColor]; self.label2.font = [UIFont systemFontOfSize:12]; label2.backgroundColor = [UIColor blueColor]; [bgView addSubview:label2]; self.count = 1; } // 开启定时器 - (void)onOpenTimer { if (self.array.count == 0) return; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 repeats:1 block:^(NSTimer * _Nonnull timer) { [UIView animateWithDuration:1 animations:^{ self.label1.frame = CGRectMake(_mainX, -_mainH, _mainW, _mainH); self.label2.frame = CGRectMake(_mainX, _mainY, _mainW, _mainH); } completion:^(BOOL finished) { self.label1.frame = CGRectMake(_mainX, _mainH, _mainW, _mainH); if (self.count < self.array.count - 1) { self.count ++; self.label1.text = self.array[self.count]; }else{ self.count = 0; //数据随机重组 [self OndataRandomReorganization]; self.label1.text = self.array[self.count]; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1 animations:^{ self.label2.frame = CGRectMake(_mainX, -_mainH, _mainW, _mainH); self.label1.frame = CGRectMake(_mainX, _mainY, _mainW, _mainH); } completion:^(BOOL finished) { self.label2.frame = CGRectMake(_mainX, _mainH, _mainW, _mainH); if (self.count < self.array.count - 1) { self.count ++; self.label2.text = self.array[self.count]; }else{ self.count = 0; //数据随机重组 [self OndataRandomReorganization]; self.label2.text = self.array[self.count]; } }]; }); }]; }]; self.timer = timer; // 开启定时器 [timer setFireDate:[NSDate distantPast]]; [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes]; } // 关闭定时器 - (void)onOffTimer { [self.timer invalidate]; } //数据随机重组 - (void)OndataRandomReorganization { self.array = [self.array sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) { int seed = arc4random_uniform(2); if (seed) { return [str1 compare:str2]; } else { return [str2 compare:str1]; } }]; } @end
引入
#import "ViewController.h" #import "ScrollUpDownView.h" @interface ViewController () @property(nonatomic, strong)ScrollUpDownView *dowmviewl; @property(nonatomic, strong)NSArray *array; @end @implementation ViewController - (NSArray *)array{ if (_array == nil) { _array = @[@"我就想说:还有谁?",@"我可以一直杀",@"国服第一JS",@"我一贱,你就笑"]; } return _array; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.dowmviewl onOpenTimer]; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [self.dowmviewl onOffTimer]; } - (void)viewDidLoad { [super viewDidLoad]; self.dowmviewl = [[ScrollUpDownView alloc]initWithFrame:CGRectMake(100, 100, 200, 15)]; self.dowmviewl.backgroundColor = [UIColor yellowColor]; self.dowmviewl.array = self.array; [self.view addSubview:self.dowmviewl]; } @end