From 45e1bd75e95676bc774ae64ec6d37bbff23fe2d0 Mon Sep 17 00:00:00 2001 From: Charlie Mezak Date: Mon, 18 Feb 2013 10:39:18 -0800 Subject: [PATCH 1/2] added invalid property to tween opration to allow cancelling --- lib/PRTween.h | 5 ++++ lib/PRTween.m | 83 +++++++++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/lib/PRTween.h b/lib/PRTween.h index 069baf5..89b0561 100755 --- a/lib/PRTween.h +++ b/lib/PRTween.h @@ -85,6 +85,8 @@ typedef void (^PRTweenCompleteBlock)(); BOOL override; + BOOL invalid; + #if NS_BLOCKS_AVAILABLE PRTweenUpdateBlock updateBlock; PRTweenCompleteBlock completeBlock; @@ -110,9 +112,12 @@ typedef void (^PRTweenCompleteBlock)(); @property (nonatomic) SEL boundGetter; @property (nonatomic) SEL boundSetter; @property (nonatomic) BOOL override; +@property (nonatomic) BOOL invalid; @end +- (void)invalidate; + @interface PRTweenCGPointLerp : NSObject + (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector; + (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector; diff --git a/lib/PRTween.m b/lib/PRTween.m index e8b39af..44ff20d 100755 --- a/lib/PRTween.m +++ b/lib/PRTween.m @@ -138,12 +138,18 @@ @implementation PRTweenOperation @synthesize boundSetter; @synthesize canUseBuiltAnimation; @synthesize override; +@synthesize invalid; #if NS_BLOCKS_AVAILABLE @synthesize updateBlock; @synthesize completeBlock; #endif +- (void)invalidate +{ + invalid = YES; +} + @end @implementation PRTweenCGPointLerp @@ -644,48 +650,55 @@ - (void)update { if (timingFunction == NULL) { timingFunction = self.defaultTimingFunction; } - - if (timingFunction != NULL && tweenOperation.canUseBuiltAnimation == NO) { - if (timeOffset <= period.startOffset + period.delay + period.duration) { - if ([period isKindOfClass:[PRTweenLerpPeriod class]]) { - if ([period conformsToProtocol:@protocol(PRTweenLerpPeriod)]) { - PRTweenLerpPeriod *lerpPeriod = (PRTweenLerpPeriod *)period; - CGFloat progress = timingFunction(timeOffset - period.startOffset - period.delay, 0.0, 1.0, period.duration); - [lerpPeriod setProgress:progress]; + + if (tweenOperation.invalid) + { + [expiredTweenOperations addObject:tweenOperation]; + } + else + { + if (timingFunction != NULL && tweenOperation.canUseBuiltAnimation == NO) { + if (timeOffset <= period.startOffset + period.delay + period.duration) { + if ([period isKindOfClass:[PRTweenLerpPeriod class]]) { + if ([period conformsToProtocol:@protocol(PRTweenLerpPeriod)]) { + PRTweenLerpPeriod *lerpPeriod = (PRTweenLerpPeriod *)period; + CGFloat progress = timingFunction(timeOffset - period.startOffset - period.delay, 0.0, 1.0, period.duration); + [lerpPeriod setProgress:progress]; + } else { + // @TODO: Throw exception + NSLog(@"Class doesn't conform to PRTweenLerp"); + } } else { - // @TODO: Throw exception - NSLog(@"Class doesn't conform to PRTweenLerp"); + // if tween operation is valid, calculate tweened value using timing function + period.tweenedValue = timingFunction(timeOffset - period.startOffset - period.delay, period.startValue, period.endValue - period.startValue, period.duration); } } else { - // if tween operation is valid, calculate tweened value using timing function - period.tweenedValue = timingFunction(timeOffset - period.startOffset - period.delay, period.startValue, period.endValue - period.startValue, period.duration); + // move expired tween operations to list for cleanup + period.tweenedValue = period.endValue; + [expiredTweenOperations addObject:tweenOperation]; } - } else { - // move expired tween operations to list for cleanup - period.tweenedValue = period.endValue; - [expiredTweenOperations addObject:tweenOperation]; - } - - NSObject *target = tweenOperation.target; - SEL selector = tweenOperation.updateSelector; - - if (period != nil) { - if (target != nil && selector != NULL) { - [target performSelector:selector withObject:period afterDelay:0]; + + NSObject *target = tweenOperation.target; + SEL selector = tweenOperation.updateSelector; + + if (period != nil) { + if (target != nil && selector != NULL) { + [target performSelector:selector withObject:period afterDelay:0]; + } + + // Check to see if blocks/GCD are supported + if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) { + // fire off update block + if (tweenOperation.updateBlock != NULL) { + tweenOperation.updateBlock(period); + } + } } - - // Check to see if blocks/GCD are supported - if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) { - // fire off update block - if (tweenOperation.updateBlock != NULL) { - tweenOperation.updateBlock(period); - } + } else if (tweenOperation.canUseBuiltAnimation == YES) { + if (timeOffset > period.startOffset + period.delay + period.duration) { + [expiredTweenOperations addObject:tweenOperation]; } } - } else if (tweenOperation.canUseBuiltAnimation == YES) { - if (timeOffset > period.startOffset + period.delay + period.duration) { - [expiredTweenOperations addObject:tweenOperation]; - } } } From aaf6f6b5061ee0be59106f4d30682089d295fc1b Mon Sep 17 00:00:00 2001 From: Charlie Mezak Date: Mon, 18 Feb 2013 11:02:50 -0800 Subject: [PATCH 2/2] fixed stupid bug --- lib/PRTween.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/PRTween.h b/lib/PRTween.h index 89b0561..f73d232 100755 --- a/lib/PRTween.h +++ b/lib/PRTween.h @@ -114,9 +114,10 @@ typedef void (^PRTweenCompleteBlock)(); @property (nonatomic) BOOL override; @property (nonatomic) BOOL invalid; +- (void)invalidate; + @end -- (void)invalidate; @interface PRTweenCGPointLerp : NSObject + (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector;