iOS开发之文字上下无限轮播滚动
2020-09-03 本文已影响0人
chasitu
我之前写了一片关于图片无限轮播的工具类,今天因为项目需要又写了一个文字上下无限轮播滚动的工具类,时间比较着急我就不多讲解了,其实就是三个控件上下位置切换,直接上代码
效果图
SHInfiniteShufflingLabel.h
#import <UIKit/UIKit.h>
typedef void(^DidSlectIndex)(NSInteger index,NSString *text);
typedef enum : NSUInteger {
SHRollingDirectionUp,
SHRollingDirectionDown,
} SHRollingDirection;
@interface SHInfiniteShufflingLabel : UIScrollView
/**
设置图片和间隔时间
@param dataArray 文字数组
@param time 间隔时间,0代表不启动定时器(自动创建和销毁定时器)
@param direction 滚动方向
*/
- (void)setTextArray:(NSArray <NSString *>*)dataArray InteralTime:(NSTimeInterval)time Direction:(SHRollingDirection)direction;
/*!
*点击回调
*/
@property (nonatomic , copy) DidSlectIndex didSelect;
@end
SHInfiniteShufflingLabel.m
#import "SHInfiniteShufflingLabel.h"
@interface SHInfiniteShufflingLabel ()<UIScrollViewDelegate>
@property (nonatomic , strong) NSMutableArray *subViewArray;
@property (nonatomic , assign) NSInteger currentPage;
@property (nonatomic , copy) NSArray *labelArray;
@end
@implementation SHInfiniteShufflingLabel
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_currentPage = 0;
self.pagingEnabled = YES;
self.bounces = NO;
self.delegate = self;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
[self removeGestureRecognizer:self.panGestureRecognizer];
}
return self;
}
- (void)setTextArray:(NSArray<NSString *> *)dataArray InteralTime:(NSTimeInterval)time Direction:(SHRollingDirection)direction
{
if (dataArray.count == 0) return;
_labelArray = dataArray; ///初始化控件
if (_labelArray.count == 1) {
UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
label.text = _labelArray.lastObject;
label.font = [UIFont boldSystemFontOfSize:13];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panClick)]];
[self addSubview:label];
}else{
self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*3);
[self setContentOffset:CGPointMake(0, self.frame.size.height)];
[self.subViewArray removeAllObjects];
for (NSInteger i = 0; i < 3; i ++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height*i, self.frame.size.width, self.frame.size.height)];
label.text = _labelArray[i == 0 ? [self getLessNum] : i == 1 ? self.currentPage : [self getMoreNum]];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panClick)]];
[self addSubview:label];
[self.subViewArray addObject:label];
}
if (_labelArray.count > 1 && time > 0) {
[NSTimer scheduledTimerWithTimeInterval:time repeats:YES block:^(NSTimer * _Nonnull timer) {
[self setContentOffset:CGPointMake(0, direction == SHRollingDirectionUp ? self.frame.size.height*2 : 0) animated:YES];
}];
}
}
}
- (void)panClick{
!self.didSelect ? : self.didSelect(self.currentPage,self.labelArray[_currentPage]);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == 0 || scrollView.contentOffset.x == 2 * self.frame.size.width) {
[self layoutSubview];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x == 0 || scrollView.contentOffset.x == 2 * self.frame.size.width) {
[self layoutSubview];
}
}
- (void)layoutSubview
{
if (self.contentOffset.y == 0) {
_currentPage = [self getLessNum];
[self.subViewArray exchangeObjectAtIndex:0 withObjectAtIndex:self.subViewArray.count-1];
[self.subViewArray exchangeObjectAtIndex:1 withObjectAtIndex:self.subViewArray.count-1];
UILabel *label = self.subViewArray.firstObject;
label.text = self.labelArray[[self getLessNum]];
}else{
_currentPage = [self getMoreNum];
[self.subViewArray exchangeObjectAtIndex:1 withObjectAtIndex:self.subViewArray.count-1];
[self.subViewArray exchangeObjectAtIndex:0 withObjectAtIndex:self.subViewArray.count-1];
UILabel *label = self.subViewArray.lastObject;
label.text = self.labelArray[[self getMoreNum]];
}
NSInteger coun = self.subViewArray.count;
for (NSInteger i = 0; i < coun; i++) {
UILabel *label = self.subViewArray[i];
label.frame = CGRectMake(0, self.frame.size.height*i, self.frame.size.width, self.frame.size.height);
}
[self setContentOffset:CGPointMake(0, self.frame.size.height)];
}
#pragma mark - lazy
- (NSMutableArray *)subViewArray
{
if (!_subViewArray) {
_subViewArray = [NSMutableArray array];
}
return _subViewArray;
}
#pragma mark - customMetod
- (NSInteger)getMoreNum{
return _currentPage == _labelArray.count-1 ? 0 : _currentPage+1;
}
- (NSInteger)getLessNum{
return _currentPage == 0 ? _labelArray.count-1 : _currentPage-1;
}
@end
使用方法如下
SHInfiniteShufflingLabel *label = [[SHInfiniteShufflingLabel alloc] initWithFrame:CGRectMake(30, 100, self.view.frame.size.width-60, 44)];
label.backgroundColor = UIColor.whiteColor;
[label setTextArray:@[@"1.上岛咖啡就是看劳动法就是盛开的积分是劳动法",
@"2.SDK和索拉卡的附近是了的开发房贷",
@"3.收快递费就SDK废旧塑料的发三楼的靠近非塑料袋开发计算量大开发就"] InteralTime:2.0 Direction:SHRollingDirectionUp];
label.didSelect = ^(NSInteger index, NSString *text) {
NSLog(@"---%ld----%@",index,text);
};
[self.view addSubview:label];
收工