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

如果各位大神发现什么问题,还望指正。
转载请注明原文链接:https://www.jianshu.com/p/6e67c879282a

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,723评论 4 61
  • 多刺激几次就会习惯成自然 有脑子并不是好事 多思多想常焦虑 需要多接受刺激 去不断扩大你的认知 认知发生改变 思维...
    乌鸦少年87阅读 2,532评论 0 0
  • @font-face是CSS3中的一个模块,他主要是把自己定义的web字体嵌入到你的网页中1、@font-face...
    MakingChoice阅读 4,873评论 0 1
  • 距离春节越来越近了, 回家的票都买了吗? 如果你春节回家选择高铁或者飞机, 这几个小妙招你一定要看。 高 铁 1想...
    旅途印记阅读 2,507评论 0 0
  • 这事情是真的,就发生在不久前,我不骗你。 是的,我有什么必要骗你呢?何况我还巴不到不让你知道呢。想骗你也不可能,因...
    Mr_稻香老农阅读 4,803评论 89 71

友情链接更多精彩内容