バツ印を回しましょう。
画面の好きなところでゆびを離してみると、
太い <-> 細い、クルクル~というアニメーションをします。
というシンプルなゲーム。
ポイント
touchesMoved, touchesEndedの二つでイベントを管理してます。
movedの方で、バツの中心を決めて、
endedのほうでアニメーションのスタートです。
幅変更、回転のアニメーションは、transformを使いました。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *barA;
UIView *barB;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
self.view.backgroundColor = [UIColor whiteColor];
[super viewDidLoad];
[self createCross];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
barA.center = p;
barB.center = p;
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self start];
}
– (void)createCross
{
barA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1200, 5)];
barA.backgroundColor = [UIColor greenColor];
barA.center = CGPointMake(self.view.bounds.size.width/2.0, 230);
[self.view addSubview:barA];
barB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 1200)];
barB.backgroundColor = [UIColor greenColor];
barB.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height / 2.0);
[self.view addSubview:barB];
}
– (void)start
{
for (int i = 0; i<3; i++) {
[self performSelector:@selector(widethin) withObject:nil afterDelay:i*0.4];
}
[self performSelector:@selector(turn) withObject:nil afterDelay:1.2];
}
– (void)widethin
{
[UIView animateWithDuration:0.2 animations:^{
barA.transform = CGAffineTransformIdentity;
barB.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
barA.transform = CGAffineTransformMakeScale(1, 20);
barB.transform = CGAffineTransformMakeScale(20, 1);
}];
}];
}
– (void)turn
{
NSArray *array = [NSArray arrayWithObjects:barA, barB, nil];
for (UIView *v in array) {
CATransform3D trans = v.layer.transform;
CABasicAnimation *turn = [CABasicAnimation animationWithKeyPath:@”transform”];
turn.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(trans, M_PI, 0, 0, 1)];
turn.repeatCount = 3;
turn.duration = 0.2;
[v.layer addAnimation:turn forKey:nil];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end