Permalink
Browse files

Added intermediate methods to specify only distance and duration of b…

…ounce. Revised full-detail method to allow setting number of bounces. Revised bounce values calculation to incorporate number of bounces, and add more frames for longer duration animations.
  • Loading branch information...
1 parent c185a0b commit f4745808afe0b968fb35f7dc2edbea06f6fce4d3 @cbpowell cbpowell committed Sep 14, 2012
Showing with 22 additions and 16 deletions.
  1. +4 −2 ViewDeck/IIViewDeckController.h
  2. +18 −14 ViewDeck/IIViewDeckController.m
@@ -104,7 +104,8 @@ typedef void (^IIViewDeckControllerBlock) (IIViewDeckController *controller);
- (BOOL)bounceLeftView;
- (BOOL)bounceLeftViewWithCompletion:(IIViewDeckControllerBlock)completed;
-- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
+- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
+- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)toggleLeftView;
- (BOOL)openLeftView;
- (BOOL)closeLeftView;
@@ -121,7 +122,8 @@ typedef void (^IIViewDeckControllerBlock) (IIViewDeckController *controller);
- (BOOL)bounceRightView;
- (BOOL)bounceRightViewWithCompletion:(IIViewDeckControllerBlock)completed;
-- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
+- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
+- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed;
- (BOOL)toggleRightView;
- (BOOL)openRightView;
- (BOOL)closeRightView;
@@ -128,7 +128,7 @@ - (BOOL)openRightViewBouncing:(IIViewDeckControllerBlock)bounced options:(UIView
- (CGRect)slidingRectForOffset:(CGFloat)offset;
- (CGSize)slidingSizeForOffset:(CGFloat)offset;
-- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration;
+- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration;
- (void)setSlidingFrameForOffset:(CGFloat)frame;
- (void)hideAppropriateSideViews;
@@ -394,7 +394,7 @@ - (void)hideAppropriateSideViews {
self.rightController.view.hidden = CGRectGetMaxX(self.slidingControllerView.frame) >= self.referenceBounds.size.width;
}
-- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration {
+- (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)left maximumBounce:(CGFloat)maxBounce numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta duration:(NSTimeInterval)duration {
// Underdamped, Free Vibration of a SDOF System
// u(t) = abs(e^(-zeta * wn * t) * ((Vo/wd) * sin(wd * t))
@@ -404,18 +404,18 @@ - (NSArray *)bouncingValuesForPosition:(CGFloat)position adjustingLeft:(BOOL)lef
// bounce can be controlled either via the initial condition Vo or the damping
// factor zeta for a desired duration, Vo is simpler mathematically.
- int steps = 150;
+ NSUInteger steps = (NSUInteger)MIN(floorf(duration * 100.0f), 100);
float time = 0.0;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps];
- double offset = 0;
- float Td = duration/2; //Damped period, equal to half the specified duration to give 4 bounces
- float wd = (2 * M_PI)/Td; // Damped frequency
+ double offset = 0.0;
+ float Td = (2.0f * duration) / numberOfBounces; //Damped period, calculated to give the number of bounces desired in the duration specified (2 bounces per Td)
+ float wd = (2.0f * M_PI)/Td; // Damped frequency
zeta = MIN(MAX(0.0001f, zeta), 0.9999f); // For an underdamped system, we must have 0 < zeta < 1
- float zetaFactor = sqrtf(1 - powf(zeta, 2.0)); // Used in multiple places
+ float zetaFactor = sqrtf(1 - powf(zeta, 2.0f)); // Used in multiple places
float wn = wd/zetaFactor; // Natural frequency
- float Vo = maxBounce * wd/(expf(-zeta/zetaFactor * (0.18 * Td) * wd) * sinf(0.18 * Td * wd));
+ float Vo = maxBounce * wd/(expf(-zeta/zetaFactor * (0.18f * Td) * wd) * sinf(0.18f * Td * wd));
for (int t = 0; t < steps; t++) {
time = (t / (float)steps) * duration;
@@ -778,10 +778,10 @@ - (BOOL)bounceLeftViewWithCompletion:(IIViewDeckControllerBlock)completed {
}
- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
- return [self bounceLeftViewToDistance:distance duration:duration dampingFactor:0.40f callDelegate:callDelegate completion:completed];
+ return [self bounceLeftViewToDistance:distance duration:duration numberOfBounces:4.0f dampingFactor:0.40f callDelegate:YES completion:completed];
}
-- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
+- (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (!self.leftController || II_FLOAT_EQUAL(CGRectGetMinX(self.slidingControllerView.frame), self.leftLedge)) return YES;
// check the delegate to allow bouncing
@@ -796,7 +796,7 @@ - (BOOL)bounceLeftViewToDistance:(CGFloat)distance duration:(NSTimeInterval)dura
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = duration;
- animation.values = [self bouncingValuesForPosition:self.slidingControllerView.layer.position.x adjustingLeft:YES maximumBounce:distance dampingFactor:zeta duration:duration];
+ animation.values = [self bouncingValuesForPosition:self.slidingControllerView.layer.position.x adjustingLeft:YES maximumBounce:distance numberOfBounces:numberOfBounces dampingFactor:zeta duration:duration];
animation.removedOnCompletion = YES;
self.leftController.view.hidden = NO;
@@ -992,10 +992,14 @@ - (BOOL)bounceRightView {
}
- (BOOL)bounceRightViewWithCompletion:(IIViewDeckControllerBlock)completed {
- return [self bounceRightViewToDistance:40.0f duration:1.2f dampingFactor:0.40f callDelegate:YES completion:completed];
+ return [self bounceRightViewToDistance:40.0f duration:1.2f callDelegate:YES completion:completed];
}
-- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
+- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
+ return [self bounceRightViewToDistance:distance duration:duration numberOfBounces:4.0f dampingFactor:0.40f callDelegate:YES completion:completed];
+}
+
+- (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)duration numberOfBounces:(CGFloat)numberOfBounces dampingFactor:(CGFloat)zeta callDelegate:(BOOL)callDelegate completion:(IIViewDeckControllerBlock)completed {
if (!self.rightController || II_FLOAT_EQUAL(CGRectGetMaxX(self.slidingControllerView.frame), self.rightLedge)) return YES;
// check the delegate to allow bouncing
@@ -1010,7 +1014,7 @@ - (BOOL)bounceRightViewToDistance:(CGFloat)distance duration:(NSTimeInterval)dur
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = duration;
- animation.values = [self bouncingValuesForPosition:self.slidingControllerView.layer.position.x adjustingLeft:NO maximumBounce:distance dampingFactor:zeta duration:duration];
+ animation.values = [self bouncingValuesForPosition:self.slidingControllerView.layer.position.x adjustingLeft:NO maximumBounce:distance numberOfBounces:numberOfBounces dampingFactor:zeta duration:duration];
animation.removedOnCompletion = YES;
self.rightController.view.hidden = NO;

0 comments on commit f474580

Please sign in to comment.