Skip to content

Commit

Permalink
Added support for bounce easing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
warrenm committed Jun 9, 2011
1 parent a651ada commit 2a6cb1a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions AHEasing/PlaygroundViewController.m
Expand Up @@ -124,6 +124,10 @@ - (void)configureEasingFunction
break;
case CurveTypeBack:
currentFunction = (currentEasing == EaseIn) ? BackEaseIn : (currentEasing == EaseOut) ? BackEaseOut : BackEaseInOut;
break;
case CurveTypeBounce:
currentFunction = (currentEasing == EaseIn) ? BounceEaseIn : (currentEasing == EaseOut) ? BounceEaseOut : BounceEaseInOut;
break;
}

[graphView setEasingFunction:currentFunction];
Expand Down
37 changes: 31 additions & 6 deletions AHEasing/easing.c
Expand Up @@ -234,19 +234,22 @@ AHFloat ElasticEaseInOut(AHFloat p)
}
}

// Modeled after the overshooting cubic y = x^3 - x*sin(x*pi)
// Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
AHFloat BackEaseIn(AHFloat p)
{
return p * p * p - p * sin(p * M_PI);
}

// Modeled after overshooting cubic y = 1 - ((1-x)^3 - (1-x)*sin((1-x)*pi))
// Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
AHFloat BackEaseOut(AHFloat p)
{
AHFloat f = (1 - p);
return 1 - (f * f * f - f * sin(f * M_PI));
}

// Modeled after the piecewise overshooting cubic function:
// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
AHFloat BackEaseInOut(AHFloat p)
{
if(p < 0.5)
Expand All @@ -256,22 +259,44 @@ AHFloat BackEaseInOut(AHFloat p)
}
else
{
AHFloat f = (1 - (2 * p - 1));
AHFloat f = (1 - (2*p - 1));
return 0.5 * (1 - (f * f * f - f * sin(f * M_PI))) + 0.5;
}
}

AHFloat BounceEaseIn(AHFloat p)
{
return p;
return 1 - BounceEaseOut(1 - p);
}

AHFloat BounceEaseOut(AHFloat p)
{
return p;
if(p < 4/11.0)
{
return (121 * p * p)/16.0;
}
else if(p < 8/11.0)
{
return (363/40.0 * p * p) - (99/10.0 * p) + 17/5.0;
}
else if(p < 9/10.0)
{
return (4356/361.0 * p * p) - (35442/1805.0 * p) + 16061/1805.0;
}
else
{
return (54/5.0 * p * p) - (513/25.0 * p) + 268/25.0;
}
}

AHFloat BounceEaseInOut(AHFloat p)
{
return p;
if(p < 0.5)
{
return 0.5 * BounceEaseIn(p*2);
}
else
{
return 0.5 * BounceEaseOut(p * 2 - 1) + 0.5;
}
}
6 changes: 3 additions & 3 deletions AHEasing/easing.h
Expand Up @@ -70,8 +70,8 @@ AHFloat BackEaseOut(AHFloat p);
AHFloat BackEaseInOut(AHFloat p);

// Exponentially-decaying bounce easing
//AHFloat BounceEaseIn(AHFloat p);
//AHFloat BounceEaseOut(AHFloat p);
//AHFloat BounceEaseInOut(AHFloat p);
AHFloat BounceEaseIn(AHFloat p);
AHFloat BounceEaseOut(AHFloat p);
AHFloat BounceEaseInOut(AHFloat p);

#endif
7 changes: 6 additions & 1 deletion AHEasing/en.lproj/PlaygroundView.xib
Expand Up @@ -88,7 +88,7 @@
<bool key="IBUIOpaque">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<int key="IBSegmentControlStyle">2</int>
<int key="IBNumberOfSegments">10</int>
<int key="IBNumberOfSegments">11</int>
<int key="IBSelectedSegmentIndex">0</int>
<object class="NSArray" key="IBSegmentTitles">
<bool key="EncodedWithXMLCoder">YES</bool>
Expand All @@ -102,6 +102,7 @@
<string>Exp</string>
<string>Elastic</string>
<string>Back</string>
<string>Bounce</string>
</object>
<object class="NSMutableArray" key="IBSegmentWidths">
<bool key="EncodedWithXMLCoder">YES</bool>
Expand All @@ -115,6 +116,7 @@
<real value="0.0"/>
<real value="0.0"/>
<real value="0.0"/>
<real value="0.0"/>
</object>
<object class="NSMutableArray" key="IBSegmentEnabledStates">
<bool key="EncodedWithXMLCoder">YES</bool>
Expand All @@ -128,6 +130,7 @@
<boolean value="YES"/>
<boolean value="YES"/>
<boolean value="YES"/>
<boolean value="YES"/>
</object>
<object class="NSMutableArray" key="IBSegmentContentOffsets">
<bool key="EncodedWithXMLCoder">YES</bool>
Expand All @@ -141,6 +144,7 @@
<string>{0, 0}</string>
<string>{0, 0}</string>
<string>{0, 0}</string>
<string>{0, 0}</string>
</object>
<object class="NSMutableArray" key="IBSegmentImages">
<bool key="EncodedWithXMLCoder">YES</bool>
Expand All @@ -154,6 +158,7 @@
<reference ref="4"/>
<reference ref="4"/>
<reference ref="4"/>
<reference ref="4"/>
</object>
</object>
<object class="IBUISegmentedControl" id="942628741">
Expand Down

0 comments on commit 2a6cb1a

Please sign in to comment.