Я хотел поэкспериментировать с анимацией в UITableViewCells, поэтому я попробовал несколько простых вещей с CALayers. У меня почти сразу возникли проблемы, и я чувствую, что мне здесь чего-то не хватает. Вот мой код.
- (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;
}
Этот код, однако, не работал должным образом. Я хотел фиолетовый квадрат, который затем увеличился бы до большего прямоугольника. Почему это не сработало?