iOS文字轮播简单实现(UILabel)
程序员文章站
2024-03-25 13:22:22
...
因项目需求,要做一个文字轮播用来展示一些通知内容,以前也有过这个需求,但之前都是在网上找的第三方,现在有点时间就自己写了个简单的。
整体思路:用一个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]];
}
}
如果各位大神发现什么问题,还望指正。