iPhoneでもTweenやってみる
ちょこちょこiPhoneアプリ開発もやってます、さとうです。
iPhoneでもTween的な動きをつけたいってことでフェードイン、フェードアウトをやってみようと思います。
Flashでよく使われているTweenerと比較して書いてみたいと思います。
まずは簡単に
- alphaを0→1に0.5秒で変化
- 1秒停止
- 1→0に0.5秒で変化
というのを書いてみたいと思います。
AS3:
var sp:Sprite = new Sprite(); sp.graphics.beginFill(0xff0000, 1); sp.graphics.drawRect(0, 0, 320, 480); sp.graphics.endFill(); sp.alpha = 0; function fadeIn() { Tweener.addTween(sp, {alpha:1, time:0.5, transition:"easeoutcubic", onComplete:fadeOut}); } function fadeOut() { Tweener.addTween(sp, {alpha:0, time:0.5, delay:1.0, transition:"easeoutcubic", onComplete:fadeOut}); } fadeIn();
Objective-C:
UIView *sp = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)]; sp.backgroundColor = [UIColor redColor]; sp.alpha = 0; - (void)fadeIn { [UIView beginAnimations:@"fadeIn" context:nil]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(fadeOut)]; [sp setAlpha:1]; [UIView commitAnimations]; } - (void)fadeOut { [UIView beginAnimations:@"fadeOut" context:nil]; [UIView setAnimationDelay:1.0f]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationDelegate:self]; [sp setAlpha:0]; [UIView commitAnimations]; } [self fadeIn];
若干書き方の違いはありますが、同じようなキーワードがあるのでなんとなく予想できますね。
Flashやってる人でもぜひ挑戦してみるといいと思います。ではでは。