#import "ViewController.h"
#import "MarqueeLabel.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen]bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen]bounds].size.height)
#define LABEL_HEIGHT 30.0f
@interface ViewController ()
{
MarqueeLabel *marquee;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = @[
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。",
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。",
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。",
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。",
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。",
@"喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。"
];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
marquee = [self setMarqueeLabel:obj];
marquee.frame = CGRectMake(0, 50 * idx + 20, SCREEN_WIDTH, LABEL_HEIGHT);
switch (idx) {
case 0:
{
marquee.marqueeType = MLContinuous;
marquee.animationCurve = UIViewAnimationOptionCurveEaseInOut;
}
break;
case 1:
{
marquee.textAlignment = NSTextAlignmentRight;
marquee.lineBreakMode = NSLineBreakByTruncatingHead;
marquee.marqueeType = MLContinuousReverse;
marquee.scrollDuration = 8.0; //滚动持续时间
marquee.fadeLength = 15.0f; //靠屏幕两边褪色长度
marquee.leadingBuffer = 40.0f; //阴影长度
}
break;
case 2:
{
marquee.marqueeType = MLLeftRight;
marquee.rate = 60.0f;
marquee.fadeLength = 10.0f;
marquee.leadingBuffer = 30.0f;
marquee.trailingBuffer = 20.0f;
marquee.textAlignment = NSTextAlignmentCenter;
}
break;
case 3:
{
marquee.marqueeType = MLRightLeft;
marquee.textAlignment = NSTextAlignmentRight;
marquee.lineBreakMode = NSLineBreakByTruncatingHead;
marquee.tapToScroll = YES;
marquee.animationDelay = 0.0f;
marquee.trailingBuffer = 20.0f;
}
break;
case 4:
{
marquee.marqueeType = MLContinuous;
marquee.scrollDuration = 10.0f;
marquee.fadeLength = 10.0f;
marquee.trailingBuffer = 30.0f;
marquee.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pauseTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[marquee addGestureRecognizer:tapRecognizer];
}
break;
case 5:
{
marquee.marqueeType = MLContinuous;
marquee.scrollDuration = 15.0f;
marquee.fadeLength = 10.0f;
marquee.trailingBuffer = 30.0f;
}
break;
default:
break;
}
[self.view addSubview:marquee];
}];
}
- (MarqueeLabel *)setMarqueeLabel:(NSString *)text {
MarqueeLabel *label = [[MarqueeLabel alloc] init];
label.backgroundColor = [UIColor blackColor];
label.text = text;
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor whiteColor];
return label;
}
- (void)pauseTap:(UITapGestureRecognizer *)recognizer {
MarqueeLabel *continuousLabel2 = (MarqueeLabel *)recognizer.view;
if (recognizer.state == UIGestureRecognizerStateEnded) {
if (!continuousLabel2.isPaused) {
[continuousLabel2 pauseLabel];
} else {
[continuousLabel2 unpauseLabel];
}
}
}
@end
效果图: