Created
January 3, 2015 15:29
-
-
Save 335g/94918cf647c5d7e1bad4 to your computer and use it in GitHub Desktop.
減衰振動(1次元)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation UIView (GRBLSpringAdditions) | |
+ (void)dampedOscillationAlongXAxis:(UIView *)view | |
toX:(CGFloat)toX | |
k:(CGFloat)k | |
omega:(CGFloat)omega | |
minimumDuration:(CGFloat)minimumDuration | |
completionBlock:(void (^)(BOOL))completionBlock { | |
/******************* | |
4回の振動で減衰終了するの前提 | |
減衰カーブ | |
y = Ae^(-kt)sin(wt) | |
2*T = duration | |
w * 0.25 * duration = 2*PI/2 | |
w * 0.50 * duration = 4*PI/2 | |
w * 0.75 * duration = 6*PI/2 | |
w * 1.00 * duration = 8*PI/2 | |
********************/ | |
CGFloat fromX = view.frame.origin.x; | |
CGFloat amplitude = fabs(toX - fromX); | |
CGFloat duration = minimumDuration * log10(amplitude + 1.0); // 1/2kx^2 = 1/2mv^2 | |
NSLog(@"duration:%f", duration); | |
CGRect lastRect = view.frame; | |
lastRect.origin.x = toX; | |
CGRect toRect1 = lastRect; | |
CGRect toRect2 = lastRect; | |
CGRect toRect3 = lastRect; | |
if (fromX - toX > 0) { | |
// Right -> Left | |
toRect1.origin.x -= amplitude * exp(-k* 2.0*M_PI/(2*omega)); | |
toRect2.origin.x += amplitude * exp(-k* 4.0*M_PI/(2*omega)); | |
toRect3.origin.x -= amplitude * exp(-k* 6.0*M_PI/(2*omega)); | |
}else { | |
// Left -> Right | |
toRect1.origin.x += amplitude * exp(-k* 2.0*M_PI/(2*omega)); | |
toRect2.origin.x -= amplitude * exp(-k* 4.0*M_PI/(2*omega)); | |
toRect3.origin.x += amplitude * exp(-k* 6.0*M_PI/(2*omega)); | |
} | |
[UIView animateWithDuration:0.25*duration | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{view.frame = toRect1;} | |
completion:^(BOOL finished){ | |
[UIView animateWithDuration:0.25*duration | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{view.frame = toRect2;} | |
completion:^(BOOL finished){ | |
[UIView animateWithDuration:0.25*duration | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{view.frame = toRect3;} | |
completion:^(BOOL finished){ | |
[UIView animateWithDuration:0.25*duration | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{view.frame = lastRect;} | |
completion:^(BOOL finished){ | |
// 終了block実行 | |
completionBlock(YES); | |
} | |
]; | |
} | |
]; | |
} | |
]; | |
} | |
]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment