1.
- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame rate:(CGFloat)pixelsPerSec andFadeLength:(CGFloat)fadeLength;
- (instancetype)initWithFrame:(CGRect)frame duration:(NSTimeInterval)scrollDuration andFadeLength:(CGFloat)fadeLength;
嵌套技巧
#pragma mark - Initialization and Label Config
- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame duration:7.0 andFadeLength:0.0];
}
- (id)initWithFrame:(CGRect)frame duration:(NSTimeInterval)aLengthOfScroll andFadeLength:(CGFloat)aFadeLength {
self = [super initWithFrame:frame];
if (self) {
[self setupLabel];
_scrollDuration = aLengthOfScroll;
self.fadeLength = MIN(aFadeLength, frame.size.width/2);
}
return self;
}
- (id)initWithFrame:(CGRect)frame rate:(CGFloat)pixelsPerSec andFadeLength:(CGFloat)aFadeLength {
self = [super initWithFrame:frame];
if (self) {
[self setupLabel];
_rate = pixelsPerSec;
self.fadeLength = MIN(aFadeLength, frame.size.width/2);
}
return self;
}
2.
极度优雅的封装:
通知表示符:
// Notification strings
NSString *const kMarqueeLabelControllerRestartNotification = @"MarqueeLabelViewControllerRestart";
NSString *const kMarqueeLabelShouldLabelizeNotification = @"MarqueeLabelShouldLabelizeNotification";
NSString *const kMarqueeLabelShouldAnimateNotification = @"MarqueeLabelShouldAnimateNotification";
NSString *const kMarqueeLabelAnimationCompletionBlock = @"MarqueeLabelAnimationCompletionBlock";
分阶段嵌套法:
+ (void)restartLabelsOfController:(UIViewController *)controller {
[MarqueeLabel notifyController:controller
withMessage:kMarqueeLabelControllerRestartNotification];
}
+ (void)controllerViewWillAppear:(UIViewController *)controller {
[MarqueeLabel restartLabelsOfController:controller];
}
+ (void)controllerViewDidAppear:(UIViewController *)controller {
[MarqueeLabel restartLabelsOfController:controller];
}
+ (void)controllerViewAppearing:(UIViewController *)controller {
[MarqueeLabel restartLabelsOfController:controller];
}
+ (void)controllerLabelsShouldLabelize:(UIViewController *)controller {
[MarqueeLabel notifyController:controller
withMessage:kMarqueeLabelShouldLabelizeNotification];
}
+ (void)controllerLabelsShouldAnimate:(UIViewController *)controller {
[MarqueeLabel notifyController:controller
withMessage:kMarqueeLabelShouldAnimateNotification];
}
+ (void)notifyController:(UIViewController *)controller withMessage:(NSString *)message
{
if (controller && message) {
[[NSNotificationCenter defaultCenter] postNotificationName:message
object:nil
userInfo:[NSDictionary dictionaryWithObject:controller
forKey:@"controller"]];
}
}
- (void)viewControllerShouldRestart:(NSNotification *)notification {
UIViewController *controller = [[notification userInfo] objectForKey:@"controller"];
if (controller == [self firstAvailableViewController]) {
[self restartLabel];
}
}
- (void)labelsShouldLabelize:(NSNotification *)notification {
UIViewController *controller = [[notification userInfo] objectForKey:@"controller"];
if (controller == [self firstAvailableViewController]) {
self.labelize = YES;
}
}
- (void)labelsShouldAnimate:(NSNotification *)notification {
UIViewController *controller = [[notification userInfo] objectForKey:@"controller"];
if (controller == [self firstAvailableViewController]) {
self.labelize = NO;
}
}
适当时期调用:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[MarqueeLabel controllerViewWillAppear:self];
}
是不是超炫酷咧!!!利用了ViewController的生命周期来分接单嵌套!!!