简单实现,主要是 CATransition
的用法
- MarqueeLabel.h实现
#import <UIKit/UIKit.h>
@class MarqueeLabel;
typedef void(^MarqueeLabelClick)(MarqueeLabel *marqueeLabel,NSInteger index);
@interface MarqueeLabel : UIView
//要显示的字符数组
@property (nonatomic, strong)NSArray<NSString *> *titlesArray;
//切换速度
@property (nonatomic, assign)CGFloat speed;
//字体
@property (nonatomic, strong)UIFont *textFont;
//字体颜色
@property (nonatomic, strong)UIColor *textColor;
//切换时间间隔
@property (nonatomic, assign)CGFloat time;
//点击回调
@property (nonatomic, copy)MarqueeLabelClick marqueeLabelClick;
- (void)end;
@end
- MarqueeLabel.m实现
#import "MarqueeLabel.h"
#define SPEED (0.5)
#define TIME (3.0)
@interface MarqueeLabel () {
UILabel *showLabel;
NSTimer *timer;
NSInteger count;
}
@end
@implementation MarqueeLabel
- (id)init {
self = [super init];
if (self) {
_textFont = [UIFont systemFontOfSize:14.0];
_textColor = [UIColor blackColor];
_time = TIME;
_speed = SPEED;
[self subViewInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_textFont = [UIFont systemFontOfSize:14.0];
_textColor = [UIColor blackColor];
_time = TIME;
_speed = SPEED;
[self subViewInit];
}
return self;
}
- (void)layoutSubviews {
showLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
- (void)subViewInit {
self.clipsToBounds = YES;
showLabel = [[UILabel alloc]init];
showLabel.textColor = _textColor;
showLabel.font = _textFont;
showLabel.clipsToBounds = YES;
showLabel.userInteractionEnabled = YES;
[self addSubview:showLabel];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelClick)];
[showLabel addGestureRecognizer:tap];
}
- (void)setTextFont:(UIFont *)textFont {
_textFont = textFont;
showLabel.font = _textFont;
}
- (void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
showLabel.textColor = _textColor;
}
- (void)setTitlesArray:(NSArray *)titlesArray {
_titlesArray = titlesArray;
if (titlesArray && titlesArray.count > 0) {
count = 0;
showLabel.text = [_titlesArray firstObject];
if (!timer) {
timer = [NSTimer timerWithTimeInterval:_time target:self selector:@selector(timer) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
} else {
if (timer) {
[timer invalidate];
timer = nil;
}
}
}
- (void)timer{
dispatch_async(dispatch_get_main_queue(), ^{
if (_titlesArray && _titlesArray.count > 0) {
count ++;
if (count >= _titlesArray.count ) {
count = 0;
}
NSString *mes = _titlesArray[count];
[self animation:mes];
}
});
}
- (void)animation:(NSString *)mes {
CATransition *animation = [CATransition animation];
//设置运动时间
animation.duration = _speed;
// kCATransitionFade
// kCATransitionMoveIn
// kCATransitionPush
// kCATransitionReveal
animation.type = kCATransitionPush;
// kCATransitionFromRight
// kCATransitionFromLeft
// kCATransitionFromTop
// kCATransitionFromBottom
animation.subtype = kCATransitionFromTop;
//设置运动速度
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
showLabel.text = mes;
[showLabel.layer addAnimation:animation forKey:@"animation"];
}
- (void)end {
if (timer) {
[timer invalidate];
timer = nil;
}
}
- (void)labelClick {
if (self.marqueeLabelClick) {
self.marqueeLabelClick(showLabel.text,count);
}
}
@end
- 使用
MarqueeLabel *label = [[MarqueeLabel alloc]initWithFrame:CGRectMake(20, 64, self.view.frame.size.width - 40, 30)];
label.titlesArray = @[@"哈哈哈哈",@"呵呵呵呵呵呵呵呵呵呵"];
label.backgroundColor = [UIColor lightGrayColor];
label.marqueeLabelClick = ^(NSString *marqueeString,NSInteger index){
NSLog(@"______%@____%ld",marqueeString,index);
};
[self.view addSubview:label];