12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517 |
- //
- // MarqueeLabel.m
- //
- // Created by Charles Powell on 1/31/11.
- // Copyright (c) 2011-2015 Charles Powell. All rights reserved.
- //
- #import "MarqueeLabel.h"
- #import <QuartzCore/QuartzCore.h>
- // Notification strings
- NSString *const kMarqueeLabelControllerRestartNotification = @"MarqueeLabelViewControllerRestart";
- NSString *const kMarqueeLabelShouldLabelizeNotification = @"MarqueeLabelShouldLabelizeNotification";
- NSString *const kMarqueeLabelShouldAnimateNotification = @"MarqueeLabelShouldAnimateNotification";
- NSString *const kMarqueeLabelAnimationCompletionBlock = @"MarqueeLabelAnimationCompletionBlock";
- // Animation completion block
- typedef void(^MLAnimationCompletionBlock)(BOOL finished);
- // iOS Version check for iOS 8.0.0
- #define SYSTEM_VERSION_IS_8_0_X ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"8.0"])
- // Helpers
- @interface UIView (MarqueeLabelHelpers)
- - (UIViewController *)firstAvailableViewController;
- - (id)traverseResponderChainForFirstViewController;
- @end
- @interface CAMediaTimingFunction (MarqueeLabelHelpers)
- - (NSArray *)controlPoints;
- - (CGFloat)durationPercentageForPositionPercentage:(CGFloat)positionPercentage withDuration:(NSTimeInterval)duration;
- @end
- @interface MarqueeLabel()
- @property (nonatomic, strong) UILabel *subLabel;
- @property (nonatomic, assign) NSTimeInterval animationDuration;
- @property (nonatomic, assign, readonly) BOOL labelShouldScroll;
- @property (nonatomic, weak) UITapGestureRecognizer *tapRecognizer;
- @property (nonatomic, assign) CGRect homeLabelFrame;
- @property (nonatomic, assign) CGFloat awayOffset;
- @property (nonatomic, assign, readwrite) BOOL isPaused;
- // Support
- @property (nonatomic, strong) NSArray *gradientColors;
- CGPoint MLOffsetCGPoint(CGPoint point, CGFloat offset);
- @end
- @implementation MarqueeLabel
- #pragma mark - Class Methods and handlers
- + (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;
- }
- }
- #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;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super initWithCoder:aDecoder];
- if (self) {
- [self setupLabel];
-
- if (self.scrollDuration == 0) {
- self.scrollDuration = 7.0;
- }
- }
- return self;
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- [self forwardPropertiesToSubLabel];
- }
- + (Class)layerClass {
- return [CAReplicatorLayer class];
- }
- - (CAReplicatorLayer *)repliLayer {
- return (CAReplicatorLayer *)self.layer;
- }
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
- // Do NOT call super, to prevent UILabel superclass from drawing into context
- // Label drawing is handled by sublabel and CAReplicatorLayer layer class
-
- // Draw only background color
- CGContextSetFillColorWithColor(ctx, self.backgroundColor.CGColor);
- CGContextFillRect(ctx, layer.bounds);
- }
- - (void)forwardPropertiesToSubLabel {
- /*
- Note that this method is currently ONLY called from awakeFromNib, i.e. when
- text properties are set via a Storyboard. As the Storyboard/IB doesn't currently
- support attributed strings, there's no need to "forward" the super attributedString value.
- */
-
- // Since we're a UILabel, we actually do implement all of UILabel's properties.
- // We don't care about these values, we just want to forward them on to our sublabel.
- NSArray *properties = @[@"baselineAdjustment", @"enabled", @"highlighted", @"highlightedTextColor",
- @"minimumFontSize", @"textAlignment",
- @"userInteractionEnabled", @"adjustsFontSizeToFitWidth",
- @"lineBreakMode", @"numberOfLines"];
-
- // Iterate through properties
- self.subLabel.text = super.text;
- self.subLabel.font = super.font;
- self.subLabel.textColor = super.textColor;
- self.subLabel.backgroundColor = (super.backgroundColor == nil ? [UIColor clearColor] : super.backgroundColor);
- self.subLabel.shadowColor = super.shadowColor;
- self.subLabel.shadowOffset = super.shadowOffset;
- for (NSString *property in properties) {
- id val = [super valueForKey:property];
- [self.subLabel setValue:val forKey:property];
- }
- }
- - (void)setupLabel {
-
- // Basic UILabel options override
- self.clipsToBounds = YES;
- self.numberOfLines = 1;
-
- // Create first sublabel
- self.subLabel = [[UILabel alloc] initWithFrame:self.bounds];
- self.subLabel.tag = 700;
- self.subLabel.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
-
- [self addSubview:self.subLabel];
-
- // Setup default values
- _awayOffset = 0.0f;
- _animationCurve = UIViewAnimationOptionCurveLinear;
- _labelize = NO;
- _holdScrolling = NO;
- _tapToScroll = NO;
- _isPaused = NO;
- _fadeLength = 0.0f;
- _animationDelay = 1.0;
- _animationDuration = 0.0f;
- _leadingBuffer = 0.0f;
- _trailingBuffer = 0.0f;
-
- // Add notification observers
- // Custom class notifications
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewControllerShouldRestart:) name:kMarqueeLabelControllerRestartNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(labelsShouldLabelize:) name:kMarqueeLabelShouldLabelizeNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(labelsShouldAnimate:) name:kMarqueeLabelShouldAnimateNotification object:nil];
-
- // UIApplication state notifications
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restartLabel) name:UIApplicationDidBecomeActiveNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shutdownLabel) name:UIApplicationDidEnterBackgroundNotification object:nil];
- }
- - (void)minimizeLabelFrameWithMaximumSize:(CGSize)maxSize adjustHeight:(BOOL)adjustHeight {
- if (self.subLabel.text != nil) {
- // Calculate text size
- if (CGSizeEqualToSize(maxSize, CGSizeZero)) {
- maxSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
- }
- CGSize minimumLabelSize = [self subLabelSize];
-
- // Adjust for fade length
- CGSize minimumSize = CGSizeMake(minimumLabelSize.width + (self.fadeLength * 2), minimumLabelSize.height);
-
- // Find minimum size of options
- minimumSize = CGSizeMake(MIN(minimumSize.width, maxSize.width), MIN(minimumSize.height, maxSize.height));
-
- // Apply to frame
- self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, minimumSize.width, (adjustHeight ? minimumSize.height : self.frame.size.height));
- }
- }
- -(void)didMoveToSuperview {
- [self updateSublabel];
- }
- #pragma mark - MarqueeLabel Heavy Lifting
- - (void)layoutSubviews
- {
- [super layoutSubviews];
-
- [self updateSublabel];
- }
- - (void)willMoveToWindow:(UIWindow *)newWindow {
- if (!newWindow) {
- [self shutdownLabel];
- }
- }
- - (void)didMoveToWindow {
- if (!self.window) {
- [self shutdownLabel];
- } else {
- [self updateSublabel];
- }
- }
- - (void)updateSublabel {
- [self updateSublabelAndBeginScroll:YES];
- }
- - (void)updateSublabelAndBeginScroll:(BOOL)beginScroll {
- if (!self.subLabel.text || !self.superview) {
- return;
- }
-
- // Calculate expected size
- CGSize expectedLabelSize = [self subLabelSize];
-
-
- // Invalidate intrinsic size
- [self invalidateIntrinsicContentSize];
-
- // Move to home
- [self returnLabelToOriginImmediately];
-
- // Configure gradient for the current condition
- [self applyGradientMaskForFadeLength:self.fadeLength animated:YES];
-
- // Check if label should scroll
- // Can be because: 1) text fits, or 2) labelization
- // The holdScrolling property does NOT affect this
- if (!self.labelShouldScroll) {
- // Set text alignment and break mode to act like normal label
- self.subLabel.textAlignment = [super textAlignment];
- self.subLabel.lineBreakMode = [super lineBreakMode];
-
- CGRect labelFrame, unusedFrame;
- switch (self.marqueeType) {
- case MLContinuousReverse:
- case MLRightLeft:
- CGRectDivide(self.bounds, &unusedFrame, &labelFrame, self.leadingBuffer, CGRectMaxXEdge);
- labelFrame = CGRectIntegral(labelFrame);
- break;
-
- default:
- labelFrame = CGRectIntegral(CGRectMake(self.leadingBuffer, 0.0f, self.bounds.size.width - self.leadingBuffer, self.bounds.size.height));
- break;
- }
-
- self.homeLabelFrame = labelFrame;
- self.awayOffset = 0.0f;
-
- // Remove an additional sublabels (for continuous types)
- self.repliLayer.instanceCount = 1;
-
- // Set sublabel frame calculated labelFrame
- self.subLabel.frame = labelFrame;
-
- return;
- }
-
- // Label DOES need to scroll
-
- [self.subLabel setLineBreakMode:NSLineBreakByClipping];
-
- // Spacing between primary and second sublabel must be at least equal to leadingBuffer, and at least equal to the fadeLength
- CGFloat minTrailing = MAX(MAX(self.leadingBuffer, self.trailingBuffer), self.fadeLength);
-
- switch (self.marqueeType) {
- case MLContinuous:
- case MLContinuousReverse:
- {
- if (self.marqueeType == MLContinuous) {
- self.homeLabelFrame = CGRectIntegral(CGRectMake(self.leadingBuffer, 0.0f, expectedLabelSize.width, self.bounds.size.height));
- self.awayOffset = -(self.homeLabelFrame.size.width + minTrailing);
- } else {
- self.homeLabelFrame = CGRectIntegral(CGRectMake(self.bounds.size.width - (expectedLabelSize.width + self.leadingBuffer), 0.0f, expectedLabelSize.width, self.bounds.size.height));
- self.awayOffset = (self.homeLabelFrame.size.width + minTrailing);
- }
-
- self.subLabel.frame = self.homeLabelFrame;
-
- // Configure replication
- self.repliLayer.instanceCount = 2;
- self.repliLayer.instanceTransform = CATransform3DMakeTranslation(-self.awayOffset, 0.0, 0.0);
-
- // Recompute the animation duration
- self.animationDuration = (self.rate != 0) ? ((NSTimeInterval) fabs(self.awayOffset) / self.rate) : (self.scrollDuration);
-
- break;
- }
-
- case MLRightLeft:
- {
- self.homeLabelFrame = CGRectIntegral(CGRectMake(self.bounds.size.width - (expectedLabelSize.width + self.leadingBuffer), 0.0f, expectedLabelSize.width, self.bounds.size.height));
- self.awayOffset = (expectedLabelSize.width + self.trailingBuffer + self.leadingBuffer) - self.bounds.size.width;
-
- // Calculate animation duration
- self.animationDuration = (self.rate != 0) ? (NSTimeInterval)fabs(self.awayOffset / self.rate) : (self.scrollDuration);
-
- // Set frame and text
- self.subLabel.frame = self.homeLabelFrame;
-
- // Remove any replication
- self.repliLayer.instanceCount = 1;
-
- // Enforce text alignment for this type
- self.subLabel.textAlignment = NSTextAlignmentRight;
-
- break;
- }
-
- case MLLeftRight:
- {
- self.homeLabelFrame = CGRectIntegral(CGRectMake(self.leadingBuffer, 0.0f, expectedLabelSize.width, expectedLabelSize.height));
- self.awayOffset = self.bounds.size.width - (expectedLabelSize.width + self.leadingBuffer + self.trailingBuffer);
-
- // Calculate animation duration
- self.animationDuration = (self.rate != 0) ? (NSTimeInterval)fabs(self.awayOffset / self.rate) : (self.scrollDuration);
-
- // Set frame
- self.subLabel.frame = self.homeLabelFrame;
-
- // Remove any replication
- self.repliLayer.instanceCount = 1;
-
- // Enforce text alignment for this type
- self.subLabel.textAlignment = NSTextAlignmentLeft;
-
- break;
- }
-
- default:
- {
- // Something strange has happened
- self.homeLabelFrame = CGRectZero;
- self.awayOffset = 0.0f;
-
- // Do not attempt to begin scroll
- return;
- break;
- }
-
- } //end of marqueeType switch
-
- if (!self.tapToScroll && !self.holdScrolling && beginScroll) {
- [self beginScroll];
- }
- }
- - (CGSize)subLabelSize {
- // Calculate expected size
- CGSize expectedLabelSize = CGSizeZero;
- CGSize maximumLabelSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
-
- // Get size of subLabel
- expectedLabelSize = [self.subLabel sizeThatFits:maximumLabelSize];
- // Sanitize width to 5461.0f (largest width a UILabel will draw on an iPhone 6S Plus)
- expectedLabelSize.width = MIN(expectedLabelSize.width, 5461.0f);
- // Adjust to own height (make text baseline match normal label)
- expectedLabelSize.height = self.bounds.size.height;
-
- return expectedLabelSize;
- }
- - (CGSize)sizeThatFits:(CGSize)size {
- CGSize fitSize = [self.subLabel sizeThatFits:size];
- fitSize.width += self.leadingBuffer;
- return fitSize;
- }
- #pragma mark - Animation Handlers
- - (BOOL)labelShouldScroll {
- BOOL stringLength = ([self.subLabel.text length] > 0);
- if (!stringLength) {
- return NO;
- }
-
- BOOL labelTooLarge = ([self subLabelSize].width + self.leadingBuffer > self.bounds.size.width);
- BOOL animationHasDuration = (self.scrollDuration > 0.0f || self.rate > 0.0f);
- return (!self.labelize && labelTooLarge && animationHasDuration);
- }
- - (BOOL)labelReadyForScroll {
- // Check if we have a superview
- if (!self.superview) {
- return NO;
- }
-
- if (!self.window) {
- return NO;
- }
-
- // Check if our view controller is ready
- UIViewController *viewController = [self firstAvailableViewController];
- if (!viewController.isViewLoaded) {
- return NO;
- }
-
- return YES;
- }
- - (void)beginScroll {
- [self beginScrollWithDelay:YES];
- }
- - (void)beginScrollWithDelay:(BOOL)delay {
- switch (self.marqueeType) {
- case MLContinuous:
- case MLContinuousReverse:
- [self scrollContinuousWithInterval:self.animationDuration after:(delay ? self.animationDelay : 0.0)];
- break;
- default:
- [self scrollAwayWithInterval:self.animationDuration];
- break;
- }
- }
- - (void)returnLabelToOriginImmediately {
- // Remove gradient animations
- [self.layer.mask removeAllAnimations];
-
- // Remove sublabel position animations
- [self.subLabel.layer removeAllAnimations];
- }
- - (void)scrollAwayWithInterval:(NSTimeInterval)interval {
- [self scrollAwayWithInterval:interval delay:YES];
- }
- - (void)scrollAwayWithInterval:(NSTimeInterval)interval delay:(BOOL)delay {
- [self scrollAwayWithInterval:interval delayAmount:(delay ? self.animationDelay : 0.0)];
- }
- - (void)scrollAwayWithInterval:(NSTimeInterval)interval delayAmount:(NSTimeInterval)delayAmount {
- // Check for conditions which would prevent scrolling
- if (![self labelReadyForScroll]) {
- return;
- }
-
- // Return labels to home (cancel any animations)
- [self returnLabelToOriginImmediately];
-
- // Call pre-animation method
- [self labelWillBeginScroll];
-
- // Animate
- [CATransaction begin];
-
- // Set Duration
- [CATransaction setAnimationDuration:(2.0 * (delayAmount + interval))];
-
- // Create animation for gradient, if needed
- if (self.fadeLength != 0.0f) {
- CAKeyframeAnimation *gradAnim = [self keyFrameAnimationForGradientFadeLength:self.fadeLength
- interval:interval
- delay:delayAmount];
- [self.layer.mask addAnimation:gradAnim forKey:@"gradient"];
- }
-
- MLAnimationCompletionBlock completionBlock = ^(BOOL finished) {
- if (!finished) {
- // Do not continue into the next loop
- return;
- }
- // Call returned home method
- [self labelReturnedToHome:YES];
- // Check to ensure that:
- // 1) We don't double fire if an animation already exists
- // 2) The instance is still attached to a window - this completion block is called for
- // many reasons, including if the animation is removed due to the view being removed
- // from the UIWindow (typically when the view controller is no longer the "top" view)
- if (self.window && ![self.subLabel.layer animationForKey:@"position"]) {
- // Begin again, if conditions met
- if (self.labelShouldScroll && !self.tapToScroll && !self.holdScrolling) {
- [self scrollAwayWithInterval:interval delayAmount:delayAmount];
- }
- }
- };
-
-
- // Create animation for position
- CGPoint homeOrigin = self.homeLabelFrame.origin;
- CGPoint awayOrigin = MLOffsetCGPoint(self.homeLabelFrame.origin, self.awayOffset);
- NSArray *values = @[[NSValue valueWithCGPoint:homeOrigin], // Initial location, home
- [NSValue valueWithCGPoint:homeOrigin], // Initial delay, at home
- [NSValue valueWithCGPoint:awayOrigin], // Animation to away
- [NSValue valueWithCGPoint:awayOrigin], // Delay at away
- [NSValue valueWithCGPoint:homeOrigin]]; // Animation to home
-
- CAKeyframeAnimation *awayAnim = [self keyFrameAnimationForProperty:@"position"
- values:values
- interval:interval
- delay:delayAmount];
- // Add completion block
- [awayAnim setValue:completionBlock forKey:kMarqueeLabelAnimationCompletionBlock];
-
- // Add animation
- [self.subLabel.layer addAnimation:awayAnim forKey:@"position"];
-
- [CATransaction commit];
- }
- - (void)scrollContinuousWithInterval:(NSTimeInterval)interval after:(NSTimeInterval)delayAmount {
- [self scrollContinuousWithInterval:interval after:delayAmount labelAnimation:nil gradientAnimation:nil];
- }
- - (void)scrollContinuousWithInterval:(NSTimeInterval)interval
- after:(NSTimeInterval)delayAmount
- labelAnimation:(CAKeyframeAnimation *)labelAnimation
- gradientAnimation:(CAKeyframeAnimation *)gradientAnimation {
- // Check for conditions which would prevent scrolling
- if (![self labelReadyForScroll]) {
- return;
- }
-
- // Return labels to home (cancel any animations)
- [self returnLabelToOriginImmediately];
-
- // Call pre-animation method
- [self labelWillBeginScroll];
-
- // Animate
- [CATransaction begin];
-
- // Set Duration
- [CATransaction setAnimationDuration:(delayAmount + interval)];
-
- // Create animation for gradient, if needed
- if (self.fadeLength != 0.0f) {
- if (!gradientAnimation) {
- gradientAnimation = [self keyFrameAnimationForGradientFadeLength:self.fadeLength
- interval:interval
- delay:delayAmount];
- }
- [self.layer.mask addAnimation:gradientAnimation forKey:@"gradient"];
- }
-
- // Create animation for sublabel positions, if needed
- if (!labelAnimation) {
- CGPoint homeOrigin = self.homeLabelFrame.origin;
- CGPoint awayOrigin = MLOffsetCGPoint(self.homeLabelFrame.origin, self.awayOffset);
- NSArray *values = @[[NSValue valueWithCGPoint:homeOrigin], // Initial location, home
- [NSValue valueWithCGPoint:homeOrigin], // Initial delay, at home
- [NSValue valueWithCGPoint:awayOrigin]]; // Animation to home
-
- labelAnimation = [self keyFrameAnimationForProperty:@"position"
- values:values
- interval:interval
- delay:delayAmount];
- }
-
- MLAnimationCompletionBlock completionBlock = ^(BOOL finished) {
- if (!finished) {
- // Do not continue into the next loop
- return;
- }
- // Call returned home method
- [self labelReturnedToHome:YES];
- // Check to ensure that:
- // 1) We don't double fire if an animation already exists
- // 2) The instance is still attached to a window - this completion block is called for
- // many reasons, including if the animation is removed due to the view being removed
- // from the UIWindow (typically when the view controller is no longer the "top" view)
- if (self.window && ![self.subLabel.layer animationForKey:@"position"]) {
- // Begin again, if conditions met
- if (self.labelShouldScroll && !self.tapToScroll && !self.holdScrolling) {
- [self scrollContinuousWithInterval:interval
- after:delayAmount
- labelAnimation:labelAnimation
- gradientAnimation:gradientAnimation];
- }
- }
- };
-
-
- // Attach completion block
- [labelAnimation setValue:completionBlock forKey:kMarqueeLabelAnimationCompletionBlock];
-
- // Add animation
- [self.subLabel.layer addAnimation:labelAnimation forKey:@"position"];
-
- [CATransaction commit];
- }
- - (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength animated:(BOOL)animated {
- // Check for zero-length fade
- if (fadeLength <= 0.0f) {
- [self removeGradientMask];
- return;
- }
-
- CAGradientLayer *gradientMask = (CAGradientLayer *)self.layer.mask;
-
- [gradientMask removeAllAnimations];
-
- if (!gradientMask) {
- // Create CAGradientLayer if needed
- gradientMask = [CAGradientLayer layer];
- }
-
- // Set up colors
- NSObject *transparent = (NSObject *)[[UIColor clearColor] CGColor];
- NSObject *opaque = (NSObject *)[[UIColor blackColor] CGColor];
-
- gradientMask.bounds = self.layer.bounds;
- gradientMask.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
- gradientMask.shouldRasterize = YES;
- gradientMask.rasterizationScale = [UIScreen mainScreen].scale;
- gradientMask.startPoint = CGPointMake(0.0f, 0.5f);
- gradientMask.endPoint = CGPointMake(1.0f, 0.5f);
- // Start with "no fade" colors and locations
- gradientMask.colors = @[opaque, opaque, opaque, opaque];
- gradientMask.locations = @[@(0.0f), @(0.0f), @(1.0f), @(1.0f)];
-
- // Set mask
- self.layer.mask = gradientMask;
-
- CGFloat leftFadeStop = fadeLength/self.bounds.size.width;
- CGFloat rightFadeStop = fadeLength/self.bounds.size.width;
-
- // Adjust stops based on fade length
- NSArray *adjustedLocations = @[@(0.0), @(leftFadeStop), @(1.0 - rightFadeStop), @(1.0)];
-
- // Determine colors for non-scrolling label (i.e. at home)
- NSArray *adjustedColors;
- BOOL trailingFadeNeeded = self.labelShouldScroll;
- switch (self.marqueeType) {
- case MLContinuousReverse:
- case MLRightLeft:
- adjustedColors = @[(trailingFadeNeeded ? transparent : opaque),
- opaque,
- opaque,
- opaque];
- break;
-
- default:
- // MLContinuous
- // MLLeftRight
- adjustedColors = @[opaque,
- opaque,
- opaque,
- (trailingFadeNeeded ? transparent : opaque)];
- break;
- }
-
- if (animated) {
- // Create animation for location change
- CABasicAnimation *locationAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
- locationAnimation.fromValue = gradientMask.locations;
- locationAnimation.toValue = adjustedLocations;
- locationAnimation.duration = 0.25;
-
- // Create animation for color change
- CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"colors"];
- colorAnimation.fromValue = gradientMask.colors;
- colorAnimation.toValue = adjustedColors;
- colorAnimation.duration = 0.25;
-
- CAAnimationGroup *group = [CAAnimationGroup animation];
- group.duration = 0.25;
- group.animations = @[locationAnimation, colorAnimation];
-
- [gradientMask addAnimation:group forKey:colorAnimation.keyPath];
- gradientMask.locations = adjustedLocations;
- gradientMask.colors = adjustedColors;
- } else {
- [CATransaction begin];
- [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
- gradientMask.locations = adjustedLocations;
- gradientMask.colors = adjustedColors;
- [CATransaction commit];
- }
- }
- - (void)removeGradientMask {
- self.layer.mask = nil;
- }
- - (CAKeyframeAnimation *)keyFrameAnimationForGradientFadeLength:(CGFloat)fadeLength
- interval:(NSTimeInterval)interval
- delay:(NSTimeInterval)delayAmount
- {
- // Setup
- NSArray *values = nil;
- NSArray *keyTimes = nil;
- NSTimeInterval totalDuration;
- NSObject *transp = (NSObject *)[[UIColor clearColor] CGColor];
- NSObject *opaque = (NSObject *)[[UIColor blackColor] CGColor];
-
- // Create new animation
- CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"colors"];
-
- // Get timing function
- CAMediaTimingFunction *timingFunction = [self timingFunctionForAnimationOptions:self.animationCurve];
-
- // Define keyTimes
- switch (self.marqueeType) {
- case MLLeftRight:
- case MLRightLeft:
- // Calculate total animation duration
- totalDuration = 2.0 * (delayAmount + interval);
- keyTimes = @[
- @(0.0), // 1) Initial gradient
- @(delayAmount/totalDuration), // 2) Begin of LE fade-in, just as scroll away starts
- @((delayAmount + 0.4)/totalDuration), // 3) End of LE fade in [LE fully faded]
- @((delayAmount + interval - 0.4)/totalDuration), // 4) Begin of TE fade out, just before scroll away finishes
- @((delayAmount + interval)/totalDuration), // 5) End of TE fade out [TE fade removed]
- @((delayAmount + interval + delayAmount)/totalDuration), // 6) Begin of TE fade back in, just as scroll home starts
- @((delayAmount + interval + delayAmount + 0.4)/totalDuration), // 7) End of TE fade back in [TE fully faded]
- @((totalDuration - 0.4)/totalDuration), // 8) Begin of LE fade out, just before scroll home finishes
- @(1.0)]; // 9) End of LE fade out, just as scroll home finishes
- break;
-
- case MLContinuousReverse:
- default:
- // Calculate total animation duration
- totalDuration = delayAmount + interval;
-
- // Find when the lead label will be totally offscreen
- CGFloat startFadeFraction = fabs((self.subLabel.bounds.size.width + self.leadingBuffer) / self.awayOffset);
- // Find when the animation will hit that point
- CGFloat startFadeTimeFraction = [timingFunction durationPercentageForPositionPercentage:startFadeFraction withDuration:totalDuration];
- NSTimeInterval startFadeTime = delayAmount + startFadeTimeFraction * interval;
-
- keyTimes = @[
- @(0.0), // Initial gradient
- @(delayAmount/totalDuration), // Begin of fade in
- @((delayAmount + 0.2)/totalDuration), // End of fade in, just as scroll away starts
- @((startFadeTime)/totalDuration), // Begin of fade out, just before scroll home completes
- @((startFadeTime + 0.1)/totalDuration), // End of fade out, as scroll home completes
- @(1.0) // Buffer final value (used on continuous types)
- ];
- break;
- }
-
- // Define gradient values
- switch (self.marqueeType) {
- case MLContinuousReverse:
- values = @[
- @[transp, opaque, opaque, opaque], // Initial gradient
- @[transp, opaque, opaque, opaque], // Begin of fade in
- @[transp, opaque, opaque, transp], // End of fade in, just as scroll away starts
- @[transp, opaque, opaque, transp], // Begin of fade out, just before scroll home completes
- @[transp, opaque, opaque, opaque], // End of fade out, as scroll home completes
- @[transp, opaque, opaque, opaque] // Final "home" value
- ];
- break;
-
- case MLRightLeft:
- values = @[
- @[transp, opaque, opaque, opaque], // 1)
- @[transp, opaque, opaque, opaque], // 2)
- @[transp, opaque, opaque, transp], // 3)
- @[transp, opaque, opaque, transp], // 4)
- @[opaque, opaque, opaque, transp], // 5)
- @[opaque, opaque, opaque, transp], // 6)
- @[transp, opaque, opaque, transp], // 7)
- @[transp, opaque, opaque, transp], // 8)
- @[transp, opaque, opaque, opaque] // 9)
- ];
- break;
-
- case MLContinuous:
- values = @[
- @[opaque, opaque, opaque, transp], // Initial gradient
- @[opaque, opaque, opaque, transp], // Begin of fade in
- @[transp, opaque, opaque, transp], // End of fade in, just as scroll away starts
- @[transp, opaque, opaque, transp], // Begin of fade out, just before scroll home completes
- @[opaque, opaque, opaque, transp], // End of fade out, as scroll home completes
- @[opaque, opaque, opaque, transp] // Final "home" value
- ];
- break;
-
- case MLLeftRight:
- default:
- values = @[
- @[opaque, opaque, opaque, transp], // 1)
- @[opaque, opaque, opaque, transp], // 2)
- @[transp, opaque, opaque, transp], // 3)
- @[transp, opaque, opaque, transp], // 4)
- @[transp, opaque, opaque, opaque], // 5)
- @[transp, opaque, opaque, opaque], // 6)
- @[transp, opaque, opaque, transp], // 7)
- @[transp, opaque, opaque, transp], // 8)
- @[opaque, opaque, opaque, transp] // 9)
- ];
- break;
- }
-
- animation.values = values;
- animation.keyTimes = keyTimes;
- animation.timingFunctions = @[timingFunction, timingFunction, timingFunction, timingFunction];
-
- return animation;
- }
- - (CAKeyframeAnimation *)keyFrameAnimationForProperty:(NSString *)property
- values:(NSArray *)values
- interval:(NSTimeInterval)interval
- delay:(NSTimeInterval)delayAmount
- {
- // Create new animation
- CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:property];
-
- // Get timing function
- CAMediaTimingFunction *timingFunction = [self timingFunctionForAnimationOptions:self.animationCurve];
-
- // Calculate times based on marqueeType
- NSTimeInterval totalDuration;
- switch (self.marqueeType) {
- case MLLeftRight:
- case MLRightLeft:
- NSAssert(values.count == 5, @"Incorrect number of values passed for MLLeftRight-type animation");
- totalDuration = 2.0 * (delayAmount + interval);
- // Set up keyTimes
- animation.keyTimes = @[@(0.0), // Initial location, home
- @(delayAmount/totalDuration), // Initial delay, at home
- @((delayAmount + interval)/totalDuration), // Animation to away
- @((delayAmount + interval + delayAmount)/totalDuration), // Delay at away
- @(1.0)]; // Animation to home
-
- animation.timingFunctions = @[timingFunction,
- timingFunction,
- timingFunction,
- timingFunction];
-
- break;
-
- // MLContinuous
- // MLContinuousReverse
- default:
- NSAssert(values.count == 3, @"Incorrect number of values passed for MLContinous-type animation");
- totalDuration = delayAmount + interval;
- // Set up keyTimes
- animation.keyTimes = @[@(0.0), // Initial location, home
- @(delayAmount/totalDuration), // Initial delay, at home
- @(1.0)]; // Animation to away
-
- animation.timingFunctions = @[timingFunction,
- timingFunction];
-
- break;
- }
-
- // Set values
- animation.values = values;
- animation.delegate = self;
-
- return animation;
- }
- - (CAMediaTimingFunction *)timingFunctionForAnimationOptions:(UIViewAnimationOptions)animationOptions {
- NSString *timingFunction;
- switch (animationOptions) {
- case UIViewAnimationOptionCurveEaseIn:
- timingFunction = kCAMediaTimingFunctionEaseIn;
- break;
-
- case UIViewAnimationOptionCurveEaseInOut:
- timingFunction = kCAMediaTimingFunctionEaseInEaseOut;
- break;
-
- case UIViewAnimationOptionCurveEaseOut:
- timingFunction = kCAMediaTimingFunctionEaseOut;
- break;
-
- default:
- timingFunction = kCAMediaTimingFunctionLinear;
- break;
- }
-
- return [CAMediaTimingFunction functionWithName:timingFunction];
- }
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
- MLAnimationCompletionBlock completionBlock = [anim valueForKey:kMarqueeLabelAnimationCompletionBlock];
- if (completionBlock) {
- completionBlock(flag);
- }
- }
- #pragma mark - Label Control
- - (void)restartLabel {
- // Shutdown the label
- [self shutdownLabel];
- // Restart scrolling if appropriate
- if (self.labelShouldScroll && !self.tapToScroll && !self.holdScrolling) {
- [self beginScroll];
- }
- }
- - (void)resetLabel {
- [self returnLabelToOriginImmediately];
- self.homeLabelFrame = CGRectNull;
- self.awayOffset = 0.0f;
- }
- - (void)shutdownLabel {
- // Bring label to home location
- [self returnLabelToOriginImmediately];
- // Apply gradient mask for home location
- [self applyGradientMaskForFadeLength:self.fadeLength animated:false];
- }
- -(void)pauseLabel
- {
- // Only pause if label is not already paused, and already in a scrolling animation
- if (!self.isPaused && self.awayFromHome) {
- // Pause sublabel position animation
- CFTimeInterval labelPauseTime = [self.subLabel.layer convertTime:CACurrentMediaTime() fromLayer:nil];
- self.subLabel.layer.speed = 0.0;
- self.subLabel.layer.timeOffset = labelPauseTime;
- // Pause gradient fade animation
- CFTimeInterval gradientPauseTime = [self.layer.mask convertTime:CACurrentMediaTime() fromLayer:nil];
- self.layer.mask.speed = 0.0;
- self.layer.mask.timeOffset = gradientPauseTime;
-
- self.isPaused = YES;
- }
- }
- -(void)unpauseLabel
- {
- if (self.isPaused) {
- // Unpause sublabel position animation
- CFTimeInterval labelPausedTime = self.subLabel.layer.timeOffset;
- self.subLabel.layer.speed = 1.0;
- self.subLabel.layer.timeOffset = 0.0;
- self.subLabel.layer.beginTime = 0.0;
- self.subLabel.layer.beginTime = [self.subLabel.layer convertTime:CACurrentMediaTime() fromLayer:nil] - labelPausedTime;
- // Unpause gradient fade animation
- CFTimeInterval gradientPauseTime = self.layer.mask.timeOffset;
- self.layer.mask.speed = 1.0;
- self.layer.mask.timeOffset = 0.0;
- self.layer.mask.beginTime = 0.0;
- self.layer.mask.beginTime = [self.layer.mask convertTime:CACurrentMediaTime() fromLayer:nil] - gradientPauseTime;
-
- self.isPaused = NO;
- }
- }
- - (void)labelWasTapped:(UITapGestureRecognizer *)recognizer {
- if (self.labelShouldScroll && !self.awayFromHome) {
- [self beginScrollWithDelay:NO];
- }
- }
- - (void)triggerScrollStart {
- if (self.labelShouldScroll && !self.awayFromHome) {
- [self beginScroll];
- }
- }
- - (void)labelWillBeginScroll {
- // Default implementation does nothing
- return;
- }
- - (void)labelReturnedToHome:(BOOL)finished {
- // Default implementation does nothing
- return;
- }
- #pragma mark - Modified UIView Methods/Getters/Setters
- - (void)setFrame:(CGRect)frame {
- [super setFrame:frame];
-
- // Check if device is running iOS 8.0.X
- if(SYSTEM_VERSION_IS_8_0_X) {
- // If so, force update because layoutSubviews is not called
- [self updateSublabel];
- }
- }
- - (void)setBounds:(CGRect)bounds {
- [super setBounds:bounds];
-
- // Check if device is running iOS 8.0.X
- if(SYSTEM_VERSION_IS_8_0_X) {
- // If so, force update because layoutSubviews is not called
- [self updateSublabel];
- }
-
- }
- #pragma mark - Modified UILabel Methods/Getters/Setters
- - (UIView *)viewForBaselineLayout {
- // Use subLabel view for handling baseline layouts
- return self.subLabel;
- }
- - (UIView *)viewForLastBaselineLayout {
- // Use subLabel view for handling baseline layouts
- return self.subLabel;
- }
- - (UIView *)viewForFirstBaselineLayout {
- // Use subLabel view for handling baseline layouts
- return self.subLabel;
- }
- - (NSString *)text {
- return self.subLabel.text;
- }
- - (void)setText:(NSString *)text {
- if ([text isEqualToString:self.subLabel.text]) {
- return;
- }
- self.subLabel.text = text;
- super.text = text;
- [self updateSublabel];
- }
- - (NSAttributedString *)attributedText {
- return self.subLabel.attributedText;
- }
- - (void)setAttributedText:(NSAttributedString *)attributedText {
- if ([attributedText isEqualToAttributedString:self.subLabel.attributedText]) {
- return;
- }
- self.subLabel.attributedText = attributedText;
- super.attributedText = attributedText;
- [self updateSublabel];
- }
- - (UIFont *)font {
- return self.subLabel.font;
- }
- - (void)setFont:(UIFont *)font {
- if ([font isEqual:self.subLabel.font]) {
- return;
- }
- self.subLabel.font = font;
- super.font = font;
- [self updateSublabel];
- }
- - (UIColor *)textColor {
- return self.subLabel.textColor;
- }
- - (void)setTextColor:(UIColor *)textColor {
- self.subLabel.textColor = textColor;
- super.textColor = textColor;
- }
- - (UIColor *)backgroundColor {
- return self.subLabel.backgroundColor;
- }
- - (void)setBackgroundColor:(UIColor *)backgroundColor {
- self.subLabel.backgroundColor = backgroundColor;
- super.backgroundColor = backgroundColor;
- }
- - (UIColor *)shadowColor {
- return self.subLabel.shadowColor;
- }
- - (void)setShadowColor:(UIColor *)shadowColor {
- self.subLabel.shadowColor = shadowColor;
- super.shadowColor = shadowColor;
- }
- - (CGSize)shadowOffset {
- return self.subLabel.shadowOffset;
- }
- - (void)setShadowOffset:(CGSize)shadowOffset {
- self.subLabel.shadowOffset = shadowOffset;
- super.shadowOffset = shadowOffset;
- }
- - (UIColor *)highlightedTextColor {
- return self.subLabel.highlightedTextColor;
- }
- - (void)setHighlightedTextColor:(UIColor *)highlightedTextColor {
- self.subLabel.highlightedTextColor = highlightedTextColor;
- super.highlightedTextColor = highlightedTextColor;
- }
- - (BOOL)isHighlighted {
- return self.subLabel.isHighlighted;
- }
- - (void)setHighlighted:(BOOL)highlighted {
- self.subLabel.highlighted = highlighted;
- super.highlighted = highlighted;
- }
- - (BOOL)isEnabled {
- return self.subLabel.isEnabled;
- }
- - (void)setEnabled:(BOOL)enabled {
- self.subLabel.enabled = enabled;
- super.enabled = enabled;
- }
- - (void)setNumberOfLines:(NSInteger)numberOfLines {
- // By the nature of MarqueeLabel, this is 1
- [super setNumberOfLines:1];
- }
- - (void)setAdjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth {
- // By the nature of MarqueeLabel, this is NO
- [super setAdjustsFontSizeToFitWidth:NO];
- }
- - (void)setMinimumFontSize:(CGFloat)minimumFontSize {
- [super setMinimumFontSize:0.0];
- }
- - (UIBaselineAdjustment)baselineAdjustment {
- return self.subLabel.baselineAdjustment;
- }
- - (void)setBaselineAdjustment:(UIBaselineAdjustment)baselineAdjustment {
- self.subLabel.baselineAdjustment = baselineAdjustment;
- super.baselineAdjustment = baselineAdjustment;
- }
- - (CGSize)intrinsicContentSize {
- CGSize contentSize = self.subLabel.intrinsicContentSize;
- contentSize.width += self.leadingBuffer;
- return contentSize;
- }
- - (void)setAdjustsLetterSpacingToFitWidth:(BOOL)adjustsLetterSpacingToFitWidth {
- // By the nature of MarqueeLabel, this is NO
- [super setAdjustsLetterSpacingToFitWidth:NO];
- }
- - (void)setMinimumScaleFactor:(CGFloat)minimumScaleFactor {
- [super setMinimumScaleFactor:0.0f];
- }
- #pragma mark - Custom Getters and Setters
- - (void)setRate:(CGFloat)rate {
- if (_rate == rate) {
- return;
- }
-
- _scrollDuration = 0.0f;
- _rate = rate;
- [self updateSublabel];
- }
- - (void)setScrollDuration:(CGFloat)lengthOfScroll {
- if (_scrollDuration == lengthOfScroll) {
- return;
- }
-
- _rate = 0.0f;
- _scrollDuration = lengthOfScroll;
- [self updateSublabel];
- }
- - (void)setAnimationCurve:(UIViewAnimationOptions)animationCurve {
- if (_animationCurve == animationCurve) {
- return;
- }
-
- NSUInteger allowableOptions = UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionCurveLinear;
- if ((allowableOptions & animationCurve) == animationCurve) {
- _animationCurve = animationCurve;
- }
- }
- - (void)setLeadingBuffer:(CGFloat)leadingBuffer {
- if (_leadingBuffer == leadingBuffer) {
- return;
- }
-
- // Do not allow negative values
- _leadingBuffer = fabs(leadingBuffer);
- [self updateSublabel];
- }
- - (void)setTrailingBuffer:(CGFloat)trailingBuffer {
- if (_trailingBuffer == trailingBuffer) {
- return;
- }
-
- // Do not allow negative values
- _trailingBuffer = fabs(trailingBuffer);
- [self updateSublabel];
- }
- - (void)setContinuousMarqueeExtraBuffer:(CGFloat)continuousMarqueeExtraBuffer {
- [self setTrailingBuffer:continuousMarqueeExtraBuffer];
- }
- - (CGFloat)continuousMarqueeExtraBuffer {
- return self.trailingBuffer;
- }
- - (void)setFadeLength:(CGFloat)fadeLength {
- if (_fadeLength == fadeLength) {
- return;
- }
-
- _fadeLength = fadeLength;
-
- [self updateSublabel];
- }
- - (void)setTapToScroll:(BOOL)tapToScroll {
- if (_tapToScroll == tapToScroll) {
- return;
- }
-
- _tapToScroll = tapToScroll;
-
- if (_tapToScroll) {
- UITapGestureRecognizer *newTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelWasTapped:)];
- [self addGestureRecognizer:newTapRecognizer];
- self.tapRecognizer = newTapRecognizer;
- self.userInteractionEnabled = YES;
- } else {
- [self removeGestureRecognizer:self.tapRecognizer];
- self.tapRecognizer = nil;
- self.userInteractionEnabled = NO;
- }
- }
- - (void)setMarqueeType:(MarqueeType)marqueeType {
- if (marqueeType == _marqueeType) {
- return;
- }
-
- _marqueeType = marqueeType;
-
- [self updateSublabel];
- }
- - (void)setLabelize:(BOOL)labelize {
- if (_labelize == labelize) {
- return;
- }
-
- _labelize = labelize;
-
- [self updateSublabelAndBeginScroll:YES];
- }
- - (void)setHoldScrolling:(BOOL)holdScrolling {
- if (_holdScrolling == holdScrolling) {
- return;
- }
-
- _holdScrolling = holdScrolling;
-
- if (!holdScrolling && !(self.awayFromHome || self.labelize || self.tapToScroll) && self.labelShouldScroll) {
- [self beginScroll];
- }
- }
- - (BOOL)awayFromHome {
- CALayer *presentationLayer = self.subLabel.layer.presentationLayer;
- if (!presentationLayer) {
- return NO;
- }
- return !(presentationLayer.position.x == self.homeLabelFrame.origin.x);
- }
- #pragma mark - Support
- - (NSArray *)gradientColors {
- if (!_gradientColors) {
- NSObject *transparent = (NSObject *)[[UIColor clearColor] CGColor];
- NSObject *opaque = (NSObject *)[[UIColor blackColor] CGColor];
- _gradientColors = [NSArray arrayWithObjects: transparent, opaque, opaque, transparent, nil];
- }
- return _gradientColors;
- }
- #pragma mark -
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- @end
- #pragma mark - Helpers
- CGPoint MLOffsetCGPoint(CGPoint point, CGFloat offset) {
- return CGPointMake(point.x + offset, point.y);
- }
- @implementation UIView (MarqueeLabelHelpers)
- // Thanks to Phil M
- // http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone
- - (id)firstAvailableViewController
- {
- // convenience function for casting and to "mask" the recursive function
- return [self traverseResponderChainForFirstViewController];
- }
- - (id)traverseResponderChainForFirstViewController
- {
- id nextResponder = [self nextResponder];
- if ([nextResponder isKindOfClass:[UIViewController class]]) {
- return nextResponder;
- } else if ([nextResponder isKindOfClass:[UIView class]]) {
- return [nextResponder traverseResponderChainForFirstViewController];
- } else {
- return nil;
- }
- }
- @end
- @implementation CAMediaTimingFunction (MarqueeLabelHelpers)
- - (CGFloat)durationPercentageForPositionPercentage:(CGFloat)positionPercentage withDuration:(NSTimeInterval)duration
- {
- // Finds the animation duration percentage that corresponds with the given animation "position" percentage.
- // Utilizes Newton's Method to solve for the parametric Bezier curve that is used by CAMediaAnimation.
-
- NSArray *controlPoints = [self controlPoints];
- CGFloat epsilon = 1.0f / (100.0f * duration);
-
- // Find the t value that gives the position percentage we want
- CGFloat t_found = [self solveTForY:positionPercentage
- withEpsilon:epsilon
- controlPoints:controlPoints];
-
- // With that t, find the corresponding animation percentage
- CGFloat durationPercentage = [self XforCurveAt:t_found withControlPoints:controlPoints];
-
- return durationPercentage;
- }
- - (CGFloat)solveTForY:(CGFloat)y_0 withEpsilon:(CGFloat)epsilon controlPoints:(NSArray *)controlPoints
- {
- // Use Newton's Method: http://en.wikipedia.org/wiki/Newton's_method
- // For first guess, use t = y (i.e. if curve were linear)
- CGFloat t0 = y_0;
- CGFloat t1 = y_0;
- CGFloat f0, df0;
-
- for (int i = 0; i < 15; i++) {
- // Base this iteration of t1 calculated from last iteration
- t0 = t1;
- // Calculate f(t0)
- f0 = [self YforCurveAt:t0 withControlPoints:controlPoints] - y_0;
- // Check if this is close (enough)
- if (fabs(f0) < epsilon) {
- // Done!
- return t0;
- }
- // Else continue Newton's Method
- df0 = [self derivativeYValueForCurveAt:t0 withControlPoints:controlPoints];
- // Check if derivative is small or zero ( http://en.wikipedia.org/wiki/Newton's_method#Failure_analysis )
- if (fabs(df0) < 1e-6) {
- NSLog(@"MarqueeLabel: Newton's Method failure, small/zero derivative!");
- break;
- }
- // Else recalculate t1
- t1 = t0 - f0/df0;
- }
-
- NSLog(@"MarqueeLabel: Failed to find t for Y input!");
- return t0;
- }
- - (CGFloat)YforCurveAt:(CGFloat)t withControlPoints:(NSArray *)controlPoints
- {
- CGPoint P0 = [controlPoints[0] CGPointValue];
- CGPoint P1 = [controlPoints[1] CGPointValue];
- CGPoint P2 = [controlPoints[2] CGPointValue];
- CGPoint P3 = [controlPoints[3] CGPointValue];
-
- // Per http://en.wikipedia.org/wiki/Bezier_curve#Cubic_B.C3.A9zier_curves
- return powf((1 - t),3) * P0.y +
- 3.0f * powf(1 - t, 2) * t * P1.y +
- 3.0f * (1 - t) * powf(t, 2) * P2.y +
- powf(t, 3) * P3.y;
-
- }
- - (CGFloat)XforCurveAt:(CGFloat)t withControlPoints:(NSArray *)controlPoints
- {
- CGPoint P0 = [controlPoints[0] CGPointValue];
- CGPoint P1 = [controlPoints[1] CGPointValue];
- CGPoint P2 = [controlPoints[2] CGPointValue];
- CGPoint P3 = [controlPoints[3] CGPointValue];
-
- // Per http://en.wikipedia.org/wiki/Bezier_curve#Cubic_B.C3.A9zier_curves
- return powf((1 - t),3) * P0.x +
- 3.0f * powf(1 - t, 2) * t * P1.x +
- 3.0f * (1 - t) * powf(t, 2) * P2.x +
- powf(t, 3) * P3.x;
-
- }
- - (CGFloat)derivativeYValueForCurveAt:(CGFloat)t withControlPoints:(NSArray *)controlPoints
- {
- CGPoint P0 = [controlPoints[0] CGPointValue];
- CGPoint P1 = [controlPoints[1] CGPointValue];
- CGPoint P2 = [controlPoints[2] CGPointValue];
- CGPoint P3 = [controlPoints[3] CGPointValue];
-
- return powf(t, 2) * (-3.0f * P0.y - 9.0f * P1.y - 9.0f * P2.y + 3.0f * P3.y) +
- t * (6.0f * P0.y + 6.0f * P2.y) +
- (-3.0f * P0.y + 3.0f * P1.y);
- }
- - (NSArray *)controlPoints
- {
- float point[2];
- NSMutableArray *pointArray = [NSMutableArray array];
- for (int i = 0; i <= 3; i++) {
- [self getControlPointAtIndex:i values:point];
- [pointArray addObject:[NSValue valueWithCGPoint:CGPointMake(point[0], point[1])]];
- }
-
- return [NSArray arrayWithArray:pointArray];
- }
- @end
|