在didSelectRowAtIndexPath上动画tableView Cell?

时间:2014-06-20 00:38:37

标签: ios objective-c uitableview

当用户选择一行时,我想调整tableView单元格的大小,单元格会再次变小然后变大。这是我到目前为止所尝试的:

#pragma mark UITableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// My animation

    cell.contentView.transform = CGAffineTransformMakeScale(0.95,0.95);
    cell.contentView.alpha = 1.f;

    [UIView beginAnimations:@"button" context:nil];
    [UIView setAnimationDuration:0.2];
    cell.contentView.transform = CGAffineTransformMakeScale(1,1);
    cell.contentView.alpha = 1.0f;
    [UIView commitAnimations];
}

这是我的tableView数据源:

#pragma mark UITableView Datasource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

// Set up cell

    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }


// Images in my cell

    NSArray *images = @[@"Songs", @"Albums", @"Artists", @"Playlists"];

    UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:images[indexPath.row]]];

    cellImage.frame = CGRectMake(0, 0, 90, 90);
    [cell addSubview:cellImage];

    return cell;
}

但这不起作用。单元格中的图像保持相同的大小。有任何想法吗?感谢。

2 个答案:

答案 0 :(得分:1)

我认真地建议用UICollectionView来做这样的事情。有了它,你只需要调用performBatchUpdates,你可以将任何框架设置放在它的块内。这实际上很神奇!

答案 1 :(得分:0)

我不知道你是否可以或应该按照你的要求去做,但如果可以的话,可能会使用自定义的UITableViewCell。但是,您可以使用自定义单元格伪造它,也可以使用包含所有单元格内容的UIView。 UIView会在单元格内部进行动画处理,如果你没有在桌面上显示分隔符,它可能看起来像单元格本身就是动画。所以基本上你是隐藏了实际的单元格,并在单元格中使用UIView模拟动画单元格。也许是这样的:

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    self.customCell.frame = CGRectMake(110, 20, 100, 20);
} completion:^(BOOL finished) {
    [UIView animateWithDuration:SLIDE_TIME delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.customCell.frame = CGRectMake(0, 0, 320, 60);
    } completion:^(BOOL finished) {


    }];
}];
相关问题