美文网首页
iOS开发之文字上下无限轮播滚动

iOS开发之文字上下无限轮播滚动

作者: chasitu | 来源:发表于2020-09-03 16:33 被阅读0次

我之前写了一片关于图片无限轮播的工具类,今天因为项目需要又写了一个文字上下无限轮播滚动的工具类,时间比较着急我就不多讲解了,其实就是三个控件上下位置切换,直接上代码


效果图

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];

收工

相关文章

  • iOS开发之文字上下无限轮播滚动

    我之前写了一片关于图片无限轮播的工具类,今天因为项目需要又写了一个文字上下无限轮播滚动的工具类,时间比较着急我就不...

  • 第五周学习内容

    焦点图轮播特效之原理、焦点图轮播样式之布局、焦点图轮播之箭头切换、焦点图轮播之无限滚动。 js简介、用法、输出。

  • 常用三方 SDCycleScrollView轮播图

    iOS图片、文字轮播器Git/SDCycleScrollView 滚动轮播图片、文字、可使用本地图片或加载网络图片...

  • 文字无限轮播

    文字无限轮播,适用于广播消息的上下无限轮播展示。 DEMO下载链接:https://github.com/IMCC...

  • iOS 文字上下滚动

    文字上下滚动 项目中可能会用到单条数据需要上下滚动显示,下面是一个非常简单的封装Demo,可以根据自己的项目进行自...

  • swift第三方控件

    iOS开发:Swift实现的轮播图、无限循环视图控件CYCircularScrollView CycleScrol...

  • iOS 关于类似于淘宝的广告上下滚动

    前言 如若项目中有类似于淘宝的广告上下滚动的需求,有可能是文字轮播,有可能是多行轮播,因为需求的变化太多,所以上下...

  • ios文字上下轮播效果

    效果图: 就是利用了两个UILabel的位置动画做出来的效果O(∩_∩)O哈哈~ 代码:https://githu...

  • iOS开发之UIScrollView无限滚动

    UIScrollView 的无限滚动主要应用在图片轮播器、欢迎界面等场景。首先需要说明的是,文本所讲的是一种"笨办...

  • Swift无限循环的图片轮播

    前言 我们常见的一些广告位、图片轮播都是可以无限轮播的,以前参考文章 iOS开发系列--无限循环的图片浏览器,自己...

网友评论

      本文标题:iOS开发之文字上下无限轮播滚动

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