私はUITableViewCellsでアニメーションを実験したかったので、CALayersで簡単なものを試しました。私はすぐに問題に遭遇し、私は本当にここで何かを逃していると感じています。ここに私のコードです。CALayerとUITableViewCell conundrum
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSString* title = [self.array objectAtIndex:indexPath.row];
cell.textLabel.text = title;
CALayer* layer = [CALayer layer];
layer.backgroundColor = [UIColor greenColor].CGColor;
layer.bounds = CGRectMake(10, 10, 50, 50);
[cell.contentView.layer addSublayer:layer];
return cell;
}
上記のコードは、期待どおり緑色の四角形を追加します。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"did select");
CALayer* layer = [CALayer layer];
layer.backgroundColor = [UIColor purpleColor].CGColor;
layer.bounds = CGRectMake(50, 10, 50, 50);
UITableViewCell* cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView.layer addSublayer:layer];
CGRect bounds = layer.bounds;
bounds.size.width = 200;
layer.bounds = bounds;
}
ただし、このコードは期待どおりに機能しませんでした。私は紫色の正方形を望んでいましたが、大きな正方形に成長しました。なぜそれは機能しませんでしたか?このラインで間違いあり