[英]Animate tableView Cell on didSelectRowAtIndexPath?
I would like to resize my tableView
cell when the user selects a row, the cell becomes smaller then larger again. 当用户选择一行时,我想调整
tableView
单元格的大小,该单元格将变小然后再变大。 This is what I have tried so far: 到目前为止,这是我尝试过的:
#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];
}
This is my tableView data source: 这是我的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;
}
But this does not work. 但这是行不通的。 The image in the cell simply stays the same size.
单元格中的图像仅保持相同大小。 Any ideas?
有任何想法吗? Thanks.
谢谢。
I seriously advise going with a UICollectionView
for something like this. 我严重建议使用
UICollectionView
的东西。 With it, you only have to call performBatchUpdates
and you can put any frame setting inside of it's block. 有了它,您只需要调用
performBatchUpdates
,就可以将任何框架设置放入其块中。 It's practically magic! 这几乎是魔术!
I don't know if you can or should do what you ask about, but if you can it would probably be with a custom UITableViewCell. 我不知道您是否可以或应该做您想做的事情,但是如果可以,那么可能是使用自定义UITableViewCell。 But, you might be able to fake it with a custom cell, maybe with a UIView that holds all the cell's contents.
但是,您也许可以使用自定义单元来伪造它,也许可以使用保存所有单元内容的UIView来伪造它。 The UIView would animate inside the cell, and if you are not showing separators on the table it could look like the cell itself is animating.
UIView会在单元格内部进行动画处理,如果您未在表格上显示分隔符,则它看起来像是单元格本身正在设置动画。 So basically you're hiding the actual cell, and using a UIView in the cell to simulate animating the cell.
因此,基本上,您是在隐藏实际的单元格,并在单元格中使用UIView模拟单元格的动画。 Maybe like this:
也许是这样的:
[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) {
}];
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.