Permalink
Browse files

Add shimmer directions.

  • Loading branch information...
1 parent d7123ed commit 7b8d632a73ac15e5a207ba0558707f6feab1c9dd @devinross committed Mar 2, 2014
Showing with 29 additions and 4 deletions.
  1. +6 −0 src/TapkuLibrary/TKShimmerLabel.h
  2. +23 −4 src/TapkuLibrary/TKShimmerLabel.m
@@ -31,8 +31,14 @@
@import UIKit;
+typedef enum {
+ TKShimmerLabelDirectionLeftToRight,
+ TKShimmerLabelDirectionRightToLeft
+} TKShimmerLabelDirection;
+
@interface TKShimmerLabel : UILabel
@property (nonatomic,strong) CAGradientLayer *textHighlightLayer;
+@property (nonatomic,assign) TKShimmerLabelDirection direction;
@end
@@ -38,6 +38,7 @@ - (id) initWithFrame:(CGRect)frame{
self.textAlignment = NSTextAlignmentCenter;
self.backgroundColor = [UIColor clearColor];
+ _direction = TKShimmerLabelDirectionLeftToRight;
id dark = (id)[UIColor colorWithWhite:1 alpha:0.40].CGColor;
id light = (id)[UIColor colorWithWhite:1 alpha:1.0f].CGColor;
@@ -60,20 +61,32 @@ - (void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+#pragma mark Private Animation Methods
+- (NSNumber*) _animationStartPoint{
+ CGFloat x = CGRectGetWidth(self.textHighlightLayer.frame)/2.0f;
+ if(self.direction == TKShimmerLabelDirectionLeftToRight)
+ return @(-x + self.frame.size.width);
+ return @(x);
+}
+- (NSNumber*) _animationEndPoint{
+ CGFloat x = CGRectGetWidth(self.textHighlightLayer.frame)/2.0f;
+ if(self.direction == TKShimmerLabelDirectionLeftToRight)
+ return @(x);
+ return @(-x + self.frame.size.width);
+}
- (void) _startShimmerAnimation{
-
if(!self.superview) return;
[self.textHighlightLayer removeAllAnimations];
- CGFloat x = self.textHighlightLayer.frame.size.width/2.0f;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
animation.repeatCount = HUGE_VALF;
- animation.toValue = @(x);
- animation.fromValue = @(-x + self.frame.size.width);
+ animation.toValue = [self _animationEndPoint];
+ animation.fromValue = [self _animationStartPoint];
animation.duration = 4.0f;
[self.textHighlightLayer addAnimation:animation forKey:@"position.x"];
}
+#pragma mark Methods That Trigger The Animation To Start
- (void) applicationDidBecomeActive:(id)sender{
[self _startShimmerAnimation];
@@ -83,5 +96,11 @@ - (void) willMoveToWindow:(UIWindow *)newWindow{
[self _startShimmerAnimation];
}
+#pragma mark Properties
+- (void) setDirection:(TKShimmerLabelDirection)direction{
+ _direction = direction;
+ [self _startShimmerAnimation];
+}
+
@end

0 comments on commit 7b8d632

Please sign in to comment.