Permalink
Browse files

updated with easier final value setting

  • Loading branch information...
1 parent 909b052 commit e7ce4384f1f751c0fe7ae099e739dad5c02c15d7 @khanlou committed Oct 11, 2012
View
BIN .DS_Store
Binary file not shown.
View
21 README.md
@@ -6,24 +6,21 @@
Basic code is simple:
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"position.y"];
+
+
+ NSString *keyPath = @"position.y";
+ id finalValue = [NSNumber numberWithFloat:300];
+ [view.layer setValue:finalValue forKeyPath:keyPath];
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
bounceAnimation.fromValue = [NSNumber numberWithFloat:view.center.x];
- bounceAnimation.toValue = [NSNumber numberWithFloat:300];
+ bounceAnimation.toValue = finalValue;
bounceAnimation.duration = 0.5f;
- bounceAnimation.delegate = self;
bounceAnimation.numberOfBounces = 2;
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
-
[view.layer addAnimation:bounceAnimation forKey:@"someKey"];
-`SKBounceAnimation` is an **explicit** animation, so you have to tell it not to remove upon completion and set the `fillMode` to `kCAFillModeForwards`. Then, in the completion delegate callback, you can set the value of the property to the final value and remove the animation.
-
- - (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
- }
+We set the value of our keypath to the final value, and then perform the animation. When the animation finishes, it is automatically removed from the layer, and the `finalValue` takes over. If you do not use `-(void)setValue:forKeyPath:`, the original value for the keyPath will take over and the animation will snap back to original location after the animation is over.
## Math
View
19 SKBounceAnimation/OneAxisBounceController.m
@@ -47,22 +47,21 @@ - (void) addBounceAnimation {
bouncingView.center = CGPointMake(160, 60);
return;
}
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"position.y"];
+
+ NSString *keyPath = @"position.y";
+ id finalValue = [NSNumber numberWithFloat:300];
+
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
bounceAnimation.fromValue = [NSNumber numberWithFloat:bouncingView.center.x];
- bounceAnimation.toValue = [NSNumber numberWithFloat:300];
+ bounceAnimation.toValue = finalValue;
bounceAnimation.duration = 0.5f;
- bounceAnimation.delegate = self;
bounceAnimation.numberOfBounces = 4;
bounceAnimation.shouldOvershoot = YES;
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
-
[bouncingView.layer addAnimation:bounceAnimation forKey:@"someKey"];
-}
-- (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
+ [bouncingView.layer setValue:finalValue forKeyPath:keyPath];
}
+
@end
View
24 SKBounceAnimation/ScaleBounceController.m
@@ -55,22 +55,22 @@ - (void) addBounceAnimation {
bouncingView.center = CGPointMake(160, 200);
return;
}
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"transform"];
+ NSString *keyPath = @"transform";
CATransform3D transform = bouncingView.layer.transform;
+ id finalValue = [NSValue valueWithCATransform3D:
+ CATransform3DScale(transform, 1.5, 1.5, 1.5)
+ ];
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
bounceAnimation.fromValue = [NSValue valueWithCATransform3D:transform];
- bounceAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(transform, 2.5, 2.5, 2.5)];
+ bounceAnimation.toValue = finalValue;
bounceAnimation.duration = 0.5f;
- bounceAnimation.numberOfBounces = 2;
- bounceAnimation.delegate = self;
-
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
-
+ bounceAnimation.numberOfBounces = 4;
+ bounceAnimation.shouldOvershoot = YES;
+
[bouncingView.layer addAnimation:bounceAnimation forKey:@"someKey"];
+
+ [bouncingView.layer setValue:finalValue forKeyPath:keyPath];
}
-- (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
-}
@end
View
29 SKBounceAnimation/ScaleRotateBounceController.m
@@ -55,24 +55,25 @@ - (void) addBounceAnimation {
bouncingView.center = CGPointMake(160, 200);
return;
}
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"transform"];
+
+
+ NSString *keyPath = @"transform";
CATransform3D transform = bouncingView.layer.transform;
+ id finalValue = [NSValue valueWithCATransform3D:
+ CATransform3DScale(CATransform3DRotate(transform, M_PI/4, 0, 0, 1), 1.5, 1.5, 1.5)
+ ];
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
bounceAnimation.fromValue = [NSValue valueWithCATransform3D:transform];
- bounceAnimation.toValue = [NSValue valueWithCATransform3D:
- CATransform3DScale(CATransform3DRotate(transform, M_PI/4, 0, 0, 1), 1.5, 1.5, 1.5)
- ];
+ bounceAnimation.toValue = finalValue;
bounceAnimation.duration = 0.5f;
- bounceAnimation.numberOfBounces = 2;
- bounceAnimation.delegate = self;
-
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
-
+ bounceAnimation.numberOfBounces = 4;
+ bounceAnimation.shouldOvershoot = YES;
+
[bouncingView.layer addAnimation:bounceAnimation forKey:@"someKey"];
-}
+
+ [bouncingView.layer setValue:finalValue forKeyPath:keyPath];
-- (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
}
+
@end
View
29 SKBounceAnimation/SizeBounceController.m
@@ -47,21 +47,22 @@ - (void) addBounceAnimation {
bouncingView.center = CGPointMake(160, 200);
return;
}
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"bounds"];
- bounceAnimation.fromValue = [NSValue valueWithCGRect:bouncingView.frame];
- bounceAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
- bounceAnimation.duration = 0.5f;
- bounceAnimation.numberOfBounces = 4;
- bounceAnimation.delegate = self;
-
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
+
+
+ NSString *sizeKeyPath = @"bounds";
+ id finalSize = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
+
+ SKBounceAnimation *sizeAnimation = [SKBounceAnimation animationWithKeyPath:sizeKeyPath];
+ sizeAnimation.fromValue = [NSValue valueWithCGRect:bouncingView.frame];
+ sizeAnimation.toValue = finalSize;
+ sizeAnimation.duration = 0.5f;
+ sizeAnimation.numberOfBounces = 4;
+ sizeAnimation.shouldOvershoot = YES;
+
+ [bouncingView.layer addAnimation:sizeAnimation forKey:@"sizeKey"];
+
+ [bouncingView.layer setValue:finalSize forKeyPath:sizeKeyPath];
- [bouncingView.layer addAnimation:bounceAnimation forKey:@"someKey"];
}
-- (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
-}
@end
View
20 SKBounceAnimation/TwoAxisBounceController.m
@@ -47,21 +47,21 @@ - (void) addBounceAnimation {
bouncingView.center = CGPointMake(60, 60);
return;
}
- SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:@"position"];
+
+ NSString *keyPath = @"position";
+ id finalValue = [NSValue valueWithCGPoint:CGPointMake(240, 340)];
+
+ SKBounceAnimation *bounceAnimation = [SKBounceAnimation animationWithKeyPath:keyPath];
bounceAnimation.fromValue = [NSValue valueWithCGPoint:bouncingView.center];
- bounceAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(240, 340)];
+ bounceAnimation.toValue = finalValue;
bounceAnimation.duration = 0.5f;
bounceAnimation.numberOfBounces = 4;
- bounceAnimation.delegate = self;
-
- bounceAnimation.removedOnCompletion = NO;
- bounceAnimation.fillMode = kCAFillModeForwards;
+ bounceAnimation.shouldOvershoot = YES;
+
[bouncingView.layer addAnimation:bounceAnimation forKey:@"someKey"];
+
+ [bouncingView.layer setValue:finalValue forKeyPath:keyPath];
}
-- (void) animationDidStop:(SKBounceAnimation *)animation finished:(BOOL)flag {
- [bouncingView.layer setValue:animation.toValue forKeyPath:animation.keyPath];
- [bouncingView.layer removeAnimationForKey:@"someKey"];
-}
@end

0 comments on commit e7ce438

Please sign in to comment.