TableViewでパラパラ漫画のように動くものがつくりたいと思い、
線が横に流れるだけの簡単なアプリをつくってみました。
ポイント
線が横に流れるように見せるため、cellのsubviewに20×20のview
を、indexPathで5ptずらしながら入れこんでます。
TableViewCellの背景は、tableView:willDisplayCell の中で
backgroundColorを指定することで赤くしています。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createTableView];
}
– (void)createTableView
{
CGRect tFrame = CGRectMake(0, 0, 300, 300);
UITableView *tv = [[UITableView alloc] initWithFrame:tFrame style:UITableViewStylePlain];
tv.center = CGPointMake(160, 240);
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
}
#pragma mark – Table view data source
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *stair = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
stair.backgroundColor = [UIColor orangeColor];
stair.transform = CGAffineTransformMakeTranslation(5 * indexPath.row, 0);
stair.alpha = 0.8;
[cell addSubview:stair];
return cell;
}
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end