60の色のなかから、好きな色をえらんで、
いっぱいスタンプをおしちゃおう!
という感じの子供向けiPhone アプリのサンプルコードを書いてみた。
ポイント
UITableViewのtransformで90度ローテーションさせることで、
横にスライドするようにしています。
選択したところが青くなると、何色か見えないので、
selectionStyleをNoneにし、cellのボーダーラインを表示にしました。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
UIColor *selected;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self createColorTable];
}
– (void)createColorTable
{
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 50, 320) style:UITableViewStylePlain];
tv.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
tv.backgroundColor = [UIColor clearColor];
tv.center = CGPointMake(160, 400);
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (selected) {
CGPoint p = [[touches anyObject] locationInView:self.view];
UIView *stamp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
stamp.backgroundColor = [UIColor clearColor];
stamp.layer.cornerRadius = 30;
stamp.layer.borderWidth = 5;
stamp.layer.borderColor = selected.CGColor;
stamp.center = p;
[self.view addSubview:stamp];
}
}
#pragma mark – table view
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 60;
}
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
– (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
float hue = indexPath.row * 1.0 / 60.0;
UIColor *color = [UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:1.0];
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
colorView.backgroundColor = color;
colorView.tag = 1;
[cell addSubview:colorView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.layer.borderColor = [UIColor darkGrayColor].CGColor;
cell.layer.borderWidth = 3;
selected = [cell viewWithTag:1].backgroundColor;
}
– (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.layer.borderWidth = 0;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end