iOS文字轮播简单实现(UILabel)

因项目需求,要做一个文字轮播用来展示一些通知内容,以前也有过这个需求,但之前都是在网上找的第三方,现在有点时间就自己写了个简单的。

整体思路:用一个UILabel来展示内容,通过UIView动画来实现滚动效果,通过控制其坐标和隐藏状态来修改其滚动的起始位置。实现比较简单,活不多说,直接贴代码了。

.h文件

#import <UIKit/UIKit.h>

@protocol YMNoticeScrollViewDelegate

@optional
/// 点击代理
- (void)noticeScrollDidClickAtIndex:(NSInteger)index content:(NSString *)content;

@end

@interface YMNoticeScrollView : UIView

@property (nonatomic, weak) id<YMNoticeScrollViewDelegate> delegate;

/// 滚动文字数组
@property (nonatomic, strong) NSArray *contents;

/// 文字停留时长,默认4S
@property (nonatomic, assign) NSTimeInterval timeInterval;

/// 文字滚动时长,默认0.3S
@property (nonatomic, assign) NSTimeInterval scrollTimeInterval;
@end

.m文件

#import "YMNoticeScrollView.h"

@implementation YMNoticeScrollView
{
    UILabel *_scrollLbl;
    NSTimer *_timer;
    NSUInteger _count;
    CGFloat Width;
    CGFloat Height;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        Width = frame.size.width;
        Height = frame.size.height;
        _timeInterval = 4;
        _scrollTimeInterval = 0.3;
        [self initSubViews];
    }
    return self;
}

/// 创建Label
- (void)initSubViews {
    self.clipsToBounds = YES;
    
    _scrollLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    _scrollLbl.textColor = TEXT_COLOR_LEVEL_2;
    _scrollLbl.font = SystemFont(12);
    _scrollLbl.numberOfLines = 2;
    _scrollLbl.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction)];
    [_scrollLbl addGestureRecognizer:tap];
    [self addSubview:_scrollLbl];
}
// 开启定时器
- (void)startTimer {
    if (!_timer) {
        _timer = [NSTimer timerWithTimeInterval:_timeInterval target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

/// 定时器方法
- (void)timerMethod {
    _count++;
    if (_count == _contents.count) {
        _count = 0;
    }
    /// 两次动画实现类似UIScrollView的滚动效果,控制坐标和隐藏状态
    [UIView animateWithDuration:_scrollTimeInterval animations:^{
        _scrollLbl.frame = CGRectMake(0, -Height, Width, Height);
    } completion:^(BOOL finished) {
        _scrollLbl.hidden = YES;
        _scrollLbl.frame = CGRectMake(0, Height, Width, Height);
        _scrollLbl.hidden = NO;
        [UIView animateWithDuration:_scrollTimeInterval animations:^{
            _scrollLbl.text = [self getCurrentContent];
            _scrollLbl.frame = CGRectMake(0, 0, Width, Height);
        } completion:^(BOOL finished) {
            
        }];
    }];
}
/// 获取要展示的内容数组
- (void)setContents:(NSArray *)contents {
    // 判断是否要重新赋值
    if ([self array:contents isEqualTo:_contents]) {
        
        return;
    }
    _contents = contents;
    [self reset];
    if (!contents || contents.count == 0) {
        return;
    }
    [self startTimer];
}

/// 重置初始状态
- (void)reset {
    
    _count = 0;
    _scrollLbl.frame = CGRectMake(0, 0, Width, Height);
    _scrollLbl.text = [self getCurrentContent];
    [_timer invalidate];
    _timer = nil;
}
/// 获取当前要展示的内容
- (NSString *)getCurrentContent {
    if (!_contents || _contents.count == 0) {
        return nil;
    }
    return _contents[_count];
}
/// 比较两个数组内容是否相同
- (BOOL)array:(NSArray *)array1 isEqualTo:(NSArray *)array2 {
    if (array1.count != array2.count) {
        return NO;
    }
    for (NSString *str in array1) {
        if (![array2 containsObject:str]) {
            return NO;
        }
    }
    return YES;
}
/// 点击事件
- (void)clickAction {
    
    if (_delegate && [(id)_delegate respondsToSelector:@selector(noticeScrollDidClickAtIndex:content:)]) {
        [_delegate noticeScrollDidClickAtIndex:_count content:_contents[_count]];
    }
}

如果各位大神发现什么问题,还望指正。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌影~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值