因項(xiàng)目需求杆查,要做一個(gè)文字輪播用來展示一些通知內(nèi)容,以前也有過這個(gè)需求臀蛛,但之前都是在網(wǎng)上找的第三方亲桦,現(xiàn)在有點(diǎn)時(shí)間就自己寫了個(gè)簡(jiǎn)單的。
整體思路:用一個(gè)UILabel來展示內(nèi)容浊仆,通過UIView動(dòng)畫來實(shí)現(xiàn)滾動(dòng)效果客峭,通過控制其坐標(biāo)和隱藏狀態(tài)來修改其滾動(dòng)的起始位置。實(shí)現(xiàn)比較簡(jiǎn)單抡柿,活不多說舔琅,直接貼代碼了。
.h文件
#import <UIKit/UIKit.h>
@protocol YMNoticeScrollViewDelegate
@optional
/// 點(diǎn)擊代理
- (void)noticeScrollDidClickAtIndex:(NSInteger)index content:(NSString *)content;
@end
@interface YMNoticeScrollView : UIView
@property (nonatomic, weak) id<YMNoticeScrollViewDelegate> delegate;
/// 滾動(dòng)文字?jǐn)?shù)組
@property (nonatomic, strong) NSArray *contents;
/// 文字停留時(shí)長(zhǎng)洲劣,默認(rèn)4S
@property (nonatomic, assign) NSTimeInterval timeInterval;
/// 文字滾動(dòng)時(shí)長(zhǎng)备蚓,默認(rèn)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;
}
/// 創(chuàng)建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];
}
// 開啟定時(shí)器
- (void)startTimer {
if (!_timer) {
_timer = [NSTimer timerWithTimeInterval:_timeInterval target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
}
/// 定時(shí)器方法
- (void)timerMethod {
_count++;
if (_count == _contents.count) {
_count = 0;
}
/// 兩次動(dòng)畫實(shí)現(xiàn)類似UIScrollView的滾動(dòng)效果,控制坐標(biāo)和隱藏狀態(tài)
[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) {
}];
}];
}
/// 獲取要展示的內(nèi)容數(shù)組
- (void)setContents:(NSArray *)contents {
// 判斷是否要重新賦值
if ([self array:contents isEqualTo:_contents]) {
return;
}
_contents = contents;
[self reset];
if (!contents || contents.count == 0) {
return;
}
[self startTimer];
}
/// 重置初始狀態(tài)
- (void)reset {
_count = 0;
_scrollLbl.frame = CGRectMake(0, 0, Width, Height);
_scrollLbl.text = [self getCurrentContent];
[_timer invalidate];
_timer = nil;
}
/// 獲取當(dāng)前要展示的內(nèi)容
- (NSString *)getCurrentContent {
if (!_contents || _contents.count == 0) {
return nil;
}
return _contents[_count];
}
/// 比較兩個(gè)數(shù)組內(nèi)容是否相同
- (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;
}
/// 點(diǎn)擊事件
- (void)clickAction {
if (_delegate && [(id)_delegate respondsToSelector:@selector(noticeScrollDidClickAtIndex:content:)]) {
[_delegate noticeScrollDidClickAtIndex:_count content:_contents[_count]];
}
}
如果各位大神發(fā)現(xiàn)什么問題囱稽,還望指正郊尝。
轉(zhuǎn)載請(qǐng)注明原文鏈接:http://www.reibang.com/p/6e67c879282a