Skip to content

Commit

Permalink
Merge pull request #23 from ludwigschubert/master
Browse files Browse the repository at this point in the history
Added support for CGSize
  • Loading branch information
domhofmann committed Aug 13, 2012
2 parents a373309 + 20603ef commit cea3ed4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,25 @@ Sometimes you will find it necessary to animate a complex value, such as a `CGPo
PRTweenCGPointLerpPeriod *period = [PRTweenCGPointLerpPeriod periodWithStartCGPoint:CGPointMake(0, 0) endCGPoint:CGPointMake(100, 100) duration:2];
```

PRTween currently has built-in lerps for `CGPoint` and `CGRect` through `PRTweenCGPointLerpPeriod` and `PRTweenCGRectLerpPeriod`. Lerps are also available as shorthands:
PRTween currently has built-in lerps for `CGPoint` and `CGRect` and `CGSize` through `PRTweenCGPointLerpPeriod`, `PRTweenCGRectLerpPeriod` and `PRTweenCGSizeLerpPeriod`. Lerps are also available as shorthands:

```objective-c
// for CGPoint
[PRTweenCGPointLerp lerp:property:from:to:duration:timingFunction:target:completeSelector:]
[PRTweenCGPointLerp lerp:property:from:to:duration:]

// for CGSize
[PRTweenCGSizeLerp lerp:property:from:to:duration:timingFunction:target:completeSelector:]
[PRTweenCGSizeLerp lerp:property:from:to:duration:]

// for CGRect
[PRTweenCGRectLerp lerp:property:from:to:duration:timingFunction:target:completeSelector:]
[PRTweenCGRectLerp lerp:property:from:to:duration:]


// blocks for iOS 4 or greater
[PRTweenCGPointLerp lerp:property:from:to:duration:timingFunction:target:updateBlock:completeBlock:]
[PRTweenCGSizeLerp lerp:property:from:to:duration:timingFunction:target:updateBlock:completeBlock:]
[PRTweenCGRectLerp lerp:property:from:to:duration:timingFunction:target:updateBlock:completeBlock:]
```

Expand Down Expand Up @@ -283,6 +288,7 @@ Contributors
* [Dominik Hofmann](https://github.com/dominikhofmann/)
* [Matt Herzog](https://github.com/mattherzog/)
* [Robert Brisita](https://github.com/transmitiveRB/)
* [Ludwig Schubert](https://github.com/ludwigschubert)

License
===
Expand Down
16 changes: 16 additions & 0 deletions lib/PRTween.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ typedef void (^PRTweenCompleteBlock)();
- (CGRect)endCGRect;
@end

@interface PRTweenCGSizeLerpPeriod : PRTweenLerpPeriod <PRTweenLerpPeriod>
+ (id)periodWithStartCGSize:(CGSize)aStartSize endCGSize:(CGSize)anEndSize duration:(CGFloat)duration;
- (CGSize)startCGSize;
- (CGSize)tweenedCGSize;
- (CGSize)endCGSize;
@end

@interface PRTweenOperation : NSObject {
PRTweenPeriod *period;
NSObject *target;
Expand Down Expand Up @@ -124,6 +131,15 @@ typedef void (^PRTweenCompleteBlock)();
#endif
@end

@interface PRTweenCGSizeLerp : NSObject
+ (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector;
+ (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector;
+ (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration;
#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)lerp:(id)object property:(NSString*)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock;
#endif
@end

@interface PRTween : NSObject {
NSMutableArray *tweenOperations;
NSMutableArray *expiredTweenOperations;
Expand Down
50 changes: 50 additions & 0 deletions lib/PRTween.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ - (void)setProgress:(CGFloat)progress {

@end

@implementation PRTweenCGSizeLerpPeriod

+ (id)periodWithStartCGSize:(CGSize)aStartSize endCGSize:(CGSize)anEndSize duration:(CGFloat)duration {
return [PRTweenCGRectLerpPeriod periodWithStartValue:[NSValue valueWithCGSize:aStartSize] endValue:[NSValue valueWithCGSize:anEndSize] duration:duration];
}

- (CGSize)startCGSize { return [self.startLerp CGSizeValue]; }
- (CGSize)tweenedCGSize { return [self.tweenedLerp CGSizeValue]; }
- (CGSize)endCGSize { return [self.endLerp CGSizeValue]; }

- (NSValue *)tweenedValueForProgress:(CGFloat)progress {

CGSize startSize = self.startCGSize;
CGSize endSize = self.endCGSize;
CGSize distance = CGSizeMake(endSize.width - startSize.width, endSize.height - startSize.height);
CGSize tweenedSize = CGSizeMake(startSize.width + distance.width * progress, startSize.height + distance.height * progress);
return [NSValue valueWithCGSize:tweenedSize];

}

- (void)setProgress:(CGFloat)progress {
self.tweenedLerp = [self tweenedValueForProgress:progress];
}

@end

@interface PRTweenOperation ()
@property (nonatomic) BOOL canUseBuiltAnimation;
@end
Expand Down Expand Up @@ -186,6 +212,30 @@ + (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect

@end

@implementation PRTweenCGSizeLerp

+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
return [PRTween lerp:object property:property period:[PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration] timingFunction:timingFunction target:target completeSelector:selector];
}

+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
PRTweenCGPointLerpPeriod *period = [PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration];
period.delay = delay;
return [PRTween lerp:object property:property period:period timingFunction:timingFunction target:target completeSelector:selector];
}

+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration {
return [PRTweenCGSizeLerp lerp:object property:property from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}

#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
return [PRTween lerp:object property:property period:[PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration] timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
#endif

@end

@interface PRTween ()
+ (SEL)setterFromProperty:(NSString *)property;
- (void)update;
Expand Down

0 comments on commit cea3ed4

Please sign in to comment.