指で上下に画面をなぞると、
カラフルなスパンコールがキラキラとんでくよ!
という感じで作った、子供向けiPhnoeアプリのサンプルコードです。
ポイント
TableViewを使ってスパンコールを飛ばしてます。
willDisplayCellをトリガーにして、
アニメーションを設定したViewをcellに追加してしてます。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createTable];
}
– (void)createTable
{
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
tv.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tv];
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 200;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30;
}
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for (int i=0; i<4; i++) {
UIView *v = [self createShuriken];
v.center = CGPointMake(v.center.x + arc4random() % 80, v.center.y);
[cell addSubview:v];
}
}
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
– (UIView*)createShuriken
{
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 10)];
[path addLineToPoint:CGPointMake(8, 8)];
[path addLineToPoint:CGPointMake(10, 0)];
[path addLineToPoint:CGPointMake(12, 8)];
[path addLineToPoint:CGPointMake(20, 10)];
[path addLineToPoint:CGPointMake(12, 12)];
[path addLineToPoint:CGPointMake(10, 20)];
[path addLineToPoint:CGPointMake(8, 12)];
[path closePath];
UIView *shuriken = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
shuriken.backgroundColor = [UIColor clearColor];
CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:shuriken.layer];
float hue = (arc4random() % 100) / 100.0;
sl.fillColor = [UIColor colorWithHue:hue saturation:1 brightness:1 alpha:1].CGColor;
sl.path = path.CGPath;
[shuriken.layer addSublayer:sl];
// animation
CABasicAnimation *turn = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];
turn.fromValue = [NSNumber numberWithFloat:0];
turn.toValue = [NSNumber numberWithFloat:M_PI * 1];
turn.duration = 0.4;
turn.repeatCount = 50;
turn.removedOnCompletion = NO;
turn.fillMode = kCAFillModeForwards;
[shuriken.layer addAnimation:turn forKey:@”rotation”];
CABasicAnimation *go = [CABasicAnimation animationWithKeyPath:@”transform.translation.x”];
go.fromValue = [NSNumber numberWithFloat:0];
go.toValue = [NSNumber numberWithFloat:420];
go.duration = 0.5 + (arc4random() % 10 / 10.0);
go.repeatCount = 1;
go.removedOnCompletion = NO;
go.fillMode = kCAFillModeForwards;
[shuriken.layer addAnimation:go forKey:@”go”];
return shuriken;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end