時間tにおけるx,yおよび縮尺を考える。
1秒間で往復する
左右:初期位置は0で -10 ~ 10の範囲
上下:初期位置は10で 0 ~ 10の範囲
縮尺:初期位置で100% 端では 80%くらいに縮める
式
x = 20 * sin(2πt)
y = abs( 20 * sin(2πt) )
ratio = 0.2 * abs(cos(2πt)) + 0.8
サンプルコード
[ EffectKun.h ]
@interface EffectKun : NSObject
+ (void)jump:(UIView*)view origin:(CGRect)frame time:(float)time;
@end
[ EffectKun.m ]
@implementation EffectKun
+ (void)jump:(UIView*)view origin:(CGRect)frame time:(float)time
{
float x = 20 * sinf(2.0 * M_PI * time) + frame.origin.x;
float y = fabsf(20 * sinf(2.0 * M_PI * time)) + frame.origin.y;
float ratio = 0.2 * fabsf(cosf(2.0 * M_PI * time)) + 0.8;
view.center = CGPointMake(x, y);
view.transform = CGAffineTransformMakeScale(1.0, ratio);
}
@end
[ ViewController.m ]
#import “ViewController.h”
#import “EffectKun.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
CADisplayLink *timer;
UIView *mark;
CGRect origin;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
mark = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 30, 30)];
mark.backgroundColor = [UIColor purpleColor];
[self.view addSubview:mark];
origin = mark.frame;
// loop
timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
[timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
– (void)update:(CADisplayLink*)sender
{
[EffectKun jump:mark origin:origin time:sender.timestamp];
}
@end