Permalink
Browse files

Run animation in SSAddressBarTextField on a the non-main thread

  • Loading branch information...
1 parent 24e1d96 commit fccf69b246e7a8f26f3900be6d1200446cea9bd6 Andrew Wang committed Apr 13, 2011
Showing with 20 additions and 10 deletions.
  1. +0 −1 SSToolkit/SSAddressBarTextFieldBackgroundView.h
  2. +20 −9 SSToolkit/SSAddressBarTextFieldBackgroundView.m
@@ -13,7 +13,6 @@
@private
BOOL _loading;
- NSTimer *_moveTimer;
SSAddressBarTextFieldBackgroundViewInnerView *_innerView;
}
@@ -14,7 +14,9 @@
static CGFloat kLoadingBackgroundWidth = 49.0f;
@interface SSAddressBarTextFieldBackgroundView (PrivateMethods)
-- (void)_moveStep:(NSTimer *)timer;
+
+- (void)_setupAnimation;
+
@end
@implementation SSAddressBarTextFieldBackgroundView
@@ -24,8 +26,6 @@ @implementation SSAddressBarTextFieldBackgroundView
#pragma mark NSObject
- (void)dealloc {
- [_moveTimer invalidate];
- [_moveTimer release];
[_innerView release];
[super dealloc];
}
@@ -45,10 +45,7 @@ - (id)initWithFrame:(CGRect)aFrame {
self.contentMode = UIViewContentModeRedraw;
_innerView = [[SSAddressBarTextFieldBackgroundViewInnerView alloc] initWithFrame:CGRectZero];
- [self addSubview:_innerView];
-
- _moveTimer = [[NSTimer alloc] initWithFireDate:[NSDate distantFuture] interval:0.08 target:self selector:@selector(_moveStep:) userInfo:nil repeats:YES];
- [[NSRunLoop mainRunLoop] addTimer:_moveTimer forMode:NSDefaultRunLoopMode];
+ [self addSubview:_innerView];
}
return self;
}
@@ -66,9 +63,24 @@ - (void)_moveStep:(NSTimer *)timer {
loadingX = loadingX + 1.0f;
_innerView.frame = CGRectMake(loadingX, self.frame.origin.y, self.frame.size.width + kLoadingBackgroundWidth, self.frame.size.height);
+
}
+- (void)_setupAnimation {
+
+ if (![_innerView.layer animationForKey:@"translation"]) {
+ _innerView.frame = CGRectMake(0, 0, self.frame.size.width + kLoadingBackgroundWidth, self.frame.size.height);
+ CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
+ translation.duration = 3;
+ translation.repeatCount = INFINITY;
+ translation.autoreverses = NO;
+ translation.fromValue = [NSNumber numberWithFloat: - kLoadingBackgroundWidth];
+ translation.toValue = [NSNumber numberWithFloat: 0.0f];
+ [_innerView.layer addAnimation:translation forKey:@"translation"];
+ }
+}
+
#pragma mark Setters
- (void)setLoading:(BOOL)isLoading {
@@ -78,10 +90,9 @@ - (void)setLoading:(BOOL)isLoading {
_loading = isLoading;
if (_loading) {
- [_moveTimer setFireDate:[NSDate date]];
+ [self _setupAnimation];
[_innerView fadeIn];
} else {
- [_moveTimer setFireDate:[NSDate distantFuture]];
[_innerView fadeOut];
}
[self setNeedsDisplay];

0 comments on commit fccf69b

Please sign in to comment.