diff --git a/JKSlidingNoteTest/JKSlidingNoteView.h b/JKSlidingNoteTest/JKSlidingNoteView.h old mode 100644 new mode 100755 index 006313a..30960c0 --- a/JKSlidingNoteTest/JKSlidingNoteView.h +++ b/JKSlidingNoteTest/JKSlidingNoteView.h @@ -14,6 +14,7 @@ typedef NS_ENUM(NSInteger, JKSlidingNoteViewScrollDirection) { @interface JKSlidingNoteView : UIView -@property (nonatomic, assign)JKSlidingNoteViewScrollDirection direction; +- (void)setData: (NSInteger)showingLinesCount timerInterval:(CGFloat)timerInterval direction:(JKSlidingNoteViewScrollDirection)scrollDirection titlesArray:(NSArray *)titlesArray; + - (instancetype)initWithFrame:(CGRect)frame showingLinesCount:(NSInteger)showingLinesCount timerInterval:(CGFloat)timerInterval direction:(JKSlidingNoteViewScrollDirection)scrollDirection titlesArray:(NSArray *)titlesArray; @end diff --git a/JKSlidingNoteTest/JKSlidingNoteView.m b/JKSlidingNoteTest/JKSlidingNoteView.m old mode 100644 new mode 100755 index 850c8a6..c271b28 --- a/JKSlidingNoteTest/JKSlidingNoteView.m +++ b/JKSlidingNoteTest/JKSlidingNoteView.m @@ -12,8 +12,10 @@ @interface JKSlidingNoteView() @property (nonatomic, strong)UITableView *tableView; @property (nonatomic, assign)CGFloat cellHeight; +@property (nonatomic, assign)CGFloat timerInterval; @property (nonatomic, strong)NSArray *titlesArray; @property (nonatomic, assign)NSInteger showingLinesCount; +@property (nonatomic, assign)JKSlidingNoteViewScrollDirection direction; @property (nonatomic, strong)NSTimer *timer; @property (nonatomic, assign)NSInteger currentIndex; @@ -21,52 +23,74 @@ @interface JKSlidingNoteView() @implementation JKSlidingNoteView +-(instancetype)initWithFrame:(CGRect)frame{ + if (self = [super initWithFrame:frame]) { + _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; + _tableView.backgroundColor = [UIColor redColor]; + _tableView.showsHorizontalScrollIndicator = NO; + _tableView.showsVerticalScrollIndicator = NO; + _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; + _tableView.userInteractionEnabled = false; + _tableView.delegate = self; + _tableView.dataSource = self; + [self addSubview:_tableView]; + } + return self; +} + - (instancetype)initWithFrame:(CGRect)frame showingLinesCount:(NSInteger)showingLinesCount timerInterval:(CGFloat)timerInterval direction:(JKSlidingNoteViewScrollDirection)scrollDirection titlesArray:(NSArray *)titlesArray{ - if (self = [super initWithFrame:frame]) { - _cellHeight = frame.size.height / (showingLinesCount * 1.0); - _titlesArray = titlesArray; - _showingLinesCount = showingLinesCount; - _direction = scrollDirection; - - _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; - _tableView.backgroundColor = [UIColor redColor]; - _tableView.showsHorizontalScrollIndicator = NO; - _tableView.showsVerticalScrollIndicator = NO; -// _tableView.userInteractionEnabled = false; - _tableView.delegate = self; - _tableView.dataSource = self; - [self addSubview:_tableView]; - - __block JKSlidingNoteView *weakSelf = self; - _timer = [NSTimer timerWithTimeInterval:timerInterval repeats:YES block:^(NSTimer * _Nonnull timer) { - if (weakSelf.direction == JKSlidingNoteViewScrollDirectionGoUp) { - weakSelf.currentIndex = weakSelf.currentIndex + 1; - if (weakSelf.currentIndex >= weakSelf.titlesArray.count * 3 - (weakSelf.showingLinesCount - 1)) { - NSLog(@"++++++++%ld",weakSelf.currentIndex); - weakSelf.currentIndex = weakSelf.titlesArray.count * 2 - weakSelf.showingLinesCount; - [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; - weakSelf.currentIndex = weakSelf.currentIndex + 1; - } - } else if (weakSelf.direction == JKSlidingNoteViewScrollDirectionGoDown) { - weakSelf.currentIndex = weakSelf.currentIndex - 1; - if (weakSelf.currentIndex < 0) { - NSLog(@"++++++++%ld",weakSelf.currentIndex); - weakSelf.currentIndex = weakSelf.titlesArray.count; - [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; - weakSelf.currentIndex = weakSelf.currentIndex - 1; - } - } - [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; - NSLog(@"____________%ld",weakSelf.currentIndex); - }]; - [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode]; - } - return self; + if (self = [self initWithFrame:frame]) { + [self setData:showingLinesCount timerInterval:timerInterval direction:scrollDirection titlesArray:titlesArray]; + } + return self; +} + +-(void)setData: (NSInteger)showingLinesCount timerInterval:(CGFloat)timerInterval direction:(JKSlidingNoteViewScrollDirection)scrollDirection titlesArray:(NSArray *)titlesArray{ + _direction = scrollDirection; + _timerInterval = timerInterval; + _showingLinesCount = showingLinesCount; + _titlesArray = titlesArray; + _cellHeight = self.frame.size.height / (_showingLinesCount * 1.0); + [_tableView reloadData]; + + if(_timer != nil){ + [_timer invalidate]; + } + + _timer = [NSTimer timerWithTimeInterval:_timerInterval target:self selector:@selector(timerFire) userInfo:nil repeats:YES]; + + [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode]; +} + +- (void)timerFire { + __block JKSlidingNoteView *weakSelf = self; + if (weakSelf.direction == JKSlidingNoteViewScrollDirectionGoUp) { + weakSelf.currentIndex = weakSelf.currentIndex + 1; + if (weakSelf.currentIndex >= weakSelf.titlesArray.count * 3 - (weakSelf.showingLinesCount - 1)) { + //NSLog(@"++++++++%ld",weakSelf.currentIndex); + weakSelf.currentIndex = weakSelf.titlesArray.count * 2 - weakSelf.showingLinesCount; + [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; + weakSelf.currentIndex = weakSelf.currentIndex + 1; + } + } else if (weakSelf.direction == JKSlidingNoteViewScrollDirectionGoDown) { + weakSelf.currentIndex = weakSelf.currentIndex - 1; + if (weakSelf.currentIndex < 0) { + //NSLog(@"++++++++%ld",weakSelf.currentIndex); + weakSelf.currentIndex = weakSelf.titlesArray.count; + [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; + weakSelf.currentIndex = weakSelf.currentIndex - 1; + } + } + [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; + //NSLog(@"____________%ld",weakSelf.currentIndex); } #pragma mark ---------- UITableViewDelegate,UITableViewDataSource ------------ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return _titlesArray.count * 3; + if(_titlesArray != nil){ + return _titlesArray.count * 3; + } + return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { diff --git a/JKSlidingNoteTest/ViewController.m b/JKSlidingNoteTest/ViewController.m index 804b724..de795cb 100644 --- a/JKSlidingNoteTest/ViewController.m +++ b/JKSlidingNoteTest/ViewController.m @@ -57,6 +57,25 @@ - (void)viewDidLoad { JKSlidingNoteView *slidingNote3 = [[JKSlidingNoteView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label3.frame), label3.frame.origin.y, [UIScreen mainScreen].bounds.size.width, label3.frame.size.height) showingLinesCount:2 timerInterval:2 direction:JKSlidingNoteViewScrollDirectionGoDown titlesArray:@[@"1:Jack is a good boy",@"2:you r right",@"3:you r right..."]]; [self.view addSubview:slidingNote3]; + + //---------------------------------------------------------------------------------------- + UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 88, 88)]; + label4.text = @"公告新闻"; + label4.textAlignment = NSTextAlignmentCenter; + label4.numberOfLines = 0; + label4.font = [UIFont boldSystemFontOfSize:25]; + label4.textColor = [UIColor whiteColor]; + label4.backgroundColor = [UIColor redColor]; + [self.view addSubview:label4]; + + JKSlidingNoteView *slidingNote4 = [[JKSlidingNoteView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label4.frame), label4.frame.origin.y, [UIScreen mainScreen].bounds.size.width, label4.frame.size.height)]; + [slidingNote4 setData:3 timerInterval:2 direction:JKSlidingNoteViewScrollDirectionGoUp titlesArray:@[@"1:Jack is a good boy",@"2:you r right",@"3:you r right..."]]; + [self.view addSubview:slidingNote4]; + + //5秒再更新数据 + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [slidingNote4 setData:3 timerInterval:2 direction:JKSlidingNoteViewScrollDirectionGoUp titlesArray:@[@"1:更新了数据1",@"2:更新了数据2",@"3:更新了数据3..."]]; + }); }