Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellable tween operations #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/PRTween.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ typedef void (^PRTweenCompleteBlock)();

BOOL override;

BOOL invalid;

#if NS_BLOCKS_AVAILABLE
PRTweenUpdateBlock updateBlock;
PRTweenCompleteBlock completeBlock;
Expand All @@ -110,9 +112,13 @@ typedef void (^PRTweenCompleteBlock)();
@property (nonatomic) SEL boundGetter;
@property (nonatomic) SEL boundSetter;
@property (nonatomic) BOOL override;
@property (nonatomic) BOOL invalid;

- (void)invalidate;

@end


@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;
Expand Down
83 changes: 48 additions & 35 deletions lib/PRTween.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <PRTweenLerpPeriod> *lerpPeriod = (PRTweenLerpPeriod <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 <PRTweenLerpPeriod> *lerpPeriod = (PRTweenLerpPeriod <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];
}
}
}

Expand Down