Skip to content

Loading…

Added support for blocks #1

Merged
merged 6 commits into from

2 participants

@mattherzog

*Added support for an update block as well as a completion block.

*Updated the example view controller to use blocks if runtime iOS version is >= 4, otherwise it will use target/selector. Tested this using iPad 3.2 as destination.

@domhofmann
Owner

Awesome, this was on my list. I'll look this over in a bit and pull over.

@domhofmann domhofmann merged commit c1a193e into domhofmann:master
@domhofmann
Owner

Sorry this took so long. Just got back from out of town.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 163 additions and 13 deletions.
  1. +1 −0 README.md
  2. +8 −2 example/PRTweenExampleAppDelegate.m
  3. +73 −5 example/PRTweenExampleViewController.m
  4. +17 −0 lib/PRTween.h
  5. +64 −6 lib/PRTween.m
View
1 README.md
@@ -91,6 +91,7 @@ Contributors
---
* [Dominik Hofmann](https://github.com/dominikhofmann/)
+* [Matt Herzog](https://github.com/mattherzog/)
License
===
View
10 example/PRTweenExampleAppDelegate.m
@@ -12,8 +12,14 @@ @implementation PRTweenExampleAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
-
- self.window.rootViewController = self.viewController;
+
+ // Check to make sure can set rootviewcontroller (available > 4.0)
+ if ([self.window respondsToSelector:@selector(setRootViewController:)]) {
+ self.window.rootViewController = self.viewController;
+ } else {
+ [self.window addSubview:self.viewController.view];
+ }
+
[self.window makeKeyAndVisible];
return YES;
}
View
78 example/PRTweenExampleViewController.m
@@ -29,21 +29,63 @@ - (IBAction)linearTapped {
[[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:904 duration:1.5];
- activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:)];
+
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0)
+ {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
+ updateBlock:^(PRTweenPeriod *period) {
+ [self update:period];
+ }
+ completionBlock:^{
+ NSLog(@"Completion");
+ }];
+
+ }
+ else
+ {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:)];
+ }
}
- (IBAction)bounceOutTapped {
[[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:904 duration:1.5];
- activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceOut];
+
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
+ updateBlock:^(PRTweenPeriod *period) {
+ [self update:period];
+ }
+ completionBlock:^{
+ NSLog(@"Completion");
+ }
+ timingFunction:&PRTweenTimingFunctionBounceOut];
+
+ } else {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceOut];
+ }
+
}
- (IBAction)bounceInTapped {
[[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:904 duration:1.5];
- activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceIn];
+
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
+ updateBlock:^(PRTweenPeriod *period) {
+ [self update:period];
+ }
+ completionBlock:^{
+ NSLog(@"Completion");
+ }
+ timingFunction:&PRTweenTimingFunctionBounceIn];
+ }
+ else {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceIn];
+ }
}
- (IBAction)bounceOutDelayTapped {
@@ -51,14 +93,40 @@ - (IBAction)bounceOutDelayTapped {
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:904 duration:1.5];
period.delay = 1.5;
- activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceOut];
+
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
+ updateBlock:^(PRTweenPeriod *period) {
+ [self update:period];
+ }
+ completionBlock:^{
+ NSLog(@"Completion");
+ }
+ timingFunction:&PRTweenTimingFunctionBounceOut];
+ }
+ else {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionBounceOut];
+ }
}
- (IBAction)quintOutTapped {
[[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:904 duration:0.6];
- activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionQuintOut];
+
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
+ updateBlock:^(PRTweenPeriod *period) {
+ [self update:period];
+ }
+ completionBlock:^{
+ NSLog(@"Completion");
+ }
+ timingFunction:&PRTweenTimingFunctionQuintOut];
+ }
+ else {
+ activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period target:self selector:@selector(update:) timingFunction:&PRTweenTimingFunctionQuintOut];
+ }
}
- (void)viewDidUnload
View
17 lib/PRTween.h
@@ -1,5 +1,6 @@
#import <Foundation/Foundation.h>
+
#import "PRTweenTimingFunctions.h"
@interface PRTweenPeriod : NSObject {
@@ -28,6 +29,11 @@
SEL updateSelector;
SEL completeSelector;
CGFloat (*timingFunction)(CGFloat, CGFloat, CGFloat, CGFloat);
+
+#if NS_BLOCKS_AVAILABLE
+ void (^completionBlock)();
+ void (^updateBlock)(PRTweenPeriod *period);
+#endif
}
@property (nonatomic, retain) PRTweenPeriod *period;
@@ -36,6 +42,11 @@
@property (nonatomic) SEL completeSelector;
@property (nonatomic, assign) CGFloat (*timingFunction)(CGFloat, CGFloat, CGFloat, CGFloat);
+#if NS_BLOCKS_AVAILABLE
+@property (nonatomic, copy) void (^updateBlock)(PRTweenPeriod *period);
+@property (nonatomic, copy) void (^completionBlock)();
+#endif
+
@end
@interface PRTween : NSObject {
@@ -49,6 +60,12 @@
+ (PRTween *)sharedInstance;
- (PRTweenOperation*)addOperation:(PRTweenOperation*)operation;
+
+#if NS_BLOCKS_AVAILABLE
+- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period updateBlock:(void (^)(PRTweenPeriod *period))updateBlock completionBlock:(void (^)())completionBlock;
+- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period updateBlock:(void (^)(PRTweenPeriod *period))updateBlock completionBlock:(void (^)())completionBlock timingFunction:(CGFloat (*)(CGFloat, CGFloat, CGFloat, CGFloat))timingFunction;
+#endif
+
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period target:(NSObject *)target selector:(SEL)selector;
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period target:(NSObject *)target selector:(SEL)selector timingFunction:(CGFloat (*)(CGFloat, CGFloat, CGFloat, CGFloat))timingFunction;
- (void)removeTweenOperation:(PRTweenOperation*)tweenOperation;
View
70 lib/PRTween.m
@@ -22,10 +22,6 @@ + (id)periodWithStartValue:(CGFloat)aStartValue endValue:(CGFloat)anEndValue dur
return [period autorelease];
}
-- (void)dealloc {
- [super dealloc];
-}
-
@end
@implementation PRTweenOperation
@@ -34,6 +30,24 @@ @implementation PRTweenOperation
@synthesize updateSelector;
@synthesize completeSelector;
@synthesize timingFunction;
+
+#if NS_BLOCKS_AVAILABLE
+
+@synthesize updateBlock;
+@synthesize completionBlock;
+
+#endif
+
+- (void)dealloc {
+#if NS_BLOCKS_AVAILABLE
+ [updateBlock release];
+ [completionBlock release];
+#endif
+ [period release];
+ [target release];
+ [super dealloc];
+}
+
@end
@interface PRTween ()
@@ -70,6 +84,28 @@ - (PRTweenOperation*)addOperation:(PRTweenOperation*)operation {
return operation;
}
+#if NS_BLOCKS_AVAILABLE
+- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period
+ updateBlock:(void (^)(PRTweenPeriod *period))updateBlock
+ completionBlock:(void (^)())completionBlock {
+ return [self addTweenPeriod:period updateBlock:updateBlock completionBlock:completionBlock timingFunction:&PRTweenTimingFunctionLinear];
+}
+
+- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period
+ updateBlock:(void (^)(PRTweenPeriod *period))anUpdateBlock
+ completionBlock:(void (^)())aCompletionBlock
+ timingFunction:(CGFloat (*)(CGFloat, CGFloat, CGFloat, CGFloat))timingFunction {
+
+ PRTweenOperation *tweenOperation = [[PRTweenOperation new] autorelease];
+ tweenOperation.period = period;
+ tweenOperation.timingFunction = timingFunction;
+ tweenOperation.updateBlock = anUpdateBlock;
+ tweenOperation.completionBlock = aCompletionBlock;
+ return [self addOperation:tweenOperation];
+
+}
+#endif
+
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period target:(NSObject *)target selector:(SEL)selector {
return [self addTweenPeriod:period target:target selector:selector timingFunction:&PRTweenTimingFunctionLinear];
}
@@ -121,8 +157,22 @@ - (void)update {
NSObject *target = tweenOperation.target;
SEL selector = tweenOperation.updateSelector;
- if (period != nil && target != nil && selector != NULL) {
- [target performSelector:selector withObject:period afterDelay:0];
+ 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) {
+ double delayInSeconds = 0.0;
+ dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
+ dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
+ // fire off update block
+ if (tweenOperation.updateBlock != NULL) {
+ tweenOperation.updateBlock(period);
+ }
+ });
+ }
}
}
}
@@ -130,6 +180,13 @@ - (void)update {
// clean up expired tween operations
for (PRTweenOperation *tweenOperation in expiredTweenOperations) {
if (tweenOperation.completeSelector) [tweenOperation.target performSelector:tweenOperation.completeSelector withObject:nil afterDelay:0];
+ // Check to see if blocks/GCD are supported
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
+ if (tweenOperation.completionBlock != NULL) {
+ tweenOperation.completionBlock();
+ }
+ }
+
[tweenOperations removeObject:tweenOperation];
tweenOperation = nil;
}
@@ -140,6 +197,7 @@ - (void)dealloc {
[tweenOperations release];
[expiredTweenOperations release];
[timer invalidate];
+
[super dealloc];
}
Something went wrong with that request. Please try again.