角度を変えてみると、ちがうものに見える不思議な絵を
目にしたことはありますか?
上下にすこーし指を動かすと、文字が変わるよ!
という感じで子供向けiPhoneアプリサンプルとして書いてみた。
ポイント
画面上に1ptのスリットを入れて、下にUITableViewを配置
偶数のCellには、Oneをデカデカと1ptのラインに分割して
奇数のCellには、Twoを同じように表示しています。
※分割は、cell自体が1ptの高さなので、ラベルの高さをindexPathを使って調整してます。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createTable];
[self createSlit];
}
– (void)createTable
{
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
tv.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tv];
}
– (void)createSlit
{
for (int i=0; i<80; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, i*2, 320, 1)];
line.backgroundColor = [UIColor blackColor];
[self.view addSubview:line];
}
}
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 1;
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 150;
}
– (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];
cell.layer.masksToBounds = YES;
cell.layer.borderWidth = 0;
if (indexPath.row % 2 == 0) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, -indexPath.row, 320, 150)];
l.font = [UIFont boldSystemFontOfSize:130];
l.text = @”ONE”;
l.textColor = [UIColor redColor];
[cell addSubview:l];
} else {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, -indexPath.row, 320, 150)];
l.font = [UIFont boldSystemFontOfSize:130];
l.text = @”TWO”;
l.textColor = [UIColor greenColor];
[cell addSubview:l];
}
return cell;
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
– (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