用户触地时UITableViewCell动画调整大小或点击单元格

时间:2014-10-31 07:33:46

标签: ios objective-c iphone uitableview

我想在显示detailViewController之前为下面的图片中的UITableViewCell设置动画(细胞调整小一些)。我在过去的两天里尝试过,并尝试了其他类似问题的答案。

一旦用户将手指放在UITableViewCell上,我希望单元格能够动画/调整大小。

enter image description here

UITableViewCell是一个自定义单元格。我已经尝试在UITableViewCell子类的setSelected:(BOOL)selected animated:(BOOL)animated方法中将一个新帧设置为cell.contentView。 UITableViewCell没有像我预期的那样调整大小。

我还观察到当用户从UITableViewCell抬起手指时调用此方法。实际上我希望一旦用户点击单元格就会发生这种情况。如果未选择单元格并且用户移动到其他某个单元格,则应将单元格调整为正常大小。

然后我做了一些关于从UITableViewCell接收触摸事件的搜索。我尝试添加UILongPressGestureRecognizer and UITapGestureRecognizer并在选择器中尝试更改cell.contentView框架

cell.contentView.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-20, cell.frame.size.height);
[cell setNeedsDisplay];

但是所选的单元格只是闪烁(单元格背景),它没有像我预期的那样调整大小。

任何建议都会非常有用。提前致谢

4 个答案:

答案 0 :(得分:3)

这是在Swift 5.0中轻按单元动画的一种简单方法:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
    }
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = .identity
    }
}

答案 1 :(得分:2)

好的通过阅读一些文档和浏览WWDC视频后,我能够解决这个问题。

我学到了很重要的东西。

永远不要在具有AutoLayoutEnabled的视图上使用setFrame:

记住这一点我改变了我的Custom UITableViewCell xib以使用AutoResizing蒙版。然后在我的UITableViewController中,我使用了这些tableView:didHighlightRowAtIndexPath: tableView:didUnHighlightRowAtIndexPath:个委托

以下是我的视图控制器中的代码段。

        -(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {

        NSLog(@"I'm Highlighted at %ld",(long)indexPath.section);
        CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];

        originalCellSize = cell.frame;

//Using animation so the frame change transform will be smooth.

        [UIView animateWithDuration:0.1f animations:^{

            cell.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-40, cell.frame.size.height);
        }];

    }


    -(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableableView cellForRowAtIndexPath:indexPath];


        [UIView animateWithDuration:0.1f animations:^{
            cell.frame = CGRectMake(0, originalCellSize.origin.y, self.view.bounds.size.width, 180) ;
        }];

        originalCellSize = CGRectMake(0, 0, 0, 0);
    }

答案 2 :(得分:2)

我试图找到类似于你的东西。我试图在我添加到自定义UITableViewCell的UIImageView中显示我的UIImage的一部分,并设置了约束以使我的imageview成为单元格的整个大小(类似于背景,特别是当你有自定义单元格时好)。我按照@Joy为this question提供的答案,创建了以下内容:

我将此添加到我的viewDidLoad

self.currentSelection = -1;
// I changed his line below, so that the image resizing (cell resizing really) is constant for 
//all my images. I guess you can use this to resize your background back and forth to whatever size you want to.
self.newCellHeight = self.tableView.frame.size.width*3/2;

然后我添加了这些:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//I added this to toggle a tap-tapagain kind of thing, so if the same cell is tapped twice, first tap expands and second tap collapses.  
    if (self.currentSelection==indexPath.row) {
        self.currentSelection = -1;
    }else{
        self.currentSelection = indexPath.row;
    }
        // save height for full text label
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];

}
//The rest is pretty much the same as his code
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    // do things with your cell here

    // sentinel
    self.currentSelection = -1;

    // animate
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = self.newCellHeight;
    } else rowHeight = 100;
    return rowHeight;
}

最后一件事,如果你想要显示背景居中,并从中心“放大”。我将我的imageview内容模式设置为中心,如下所示:

[cell.myImageView setContentMode:UIViewContentModeCenter];

我从@EmptyStack和@erkanyildiz得到了很多答案this question

无论如何,我希望无论如何这都有帮助,如果没有相关的话,对不起! :)

答案 3 :(得分:-1)

试试这个

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath)path
// or it must be some other method
{
  [self.tableView beginUpdates];
  [[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:rowAnimation];
  [self.tableView endUpdates];
}

在表格视图委托方法中,您必须知道选择了哪个单元格。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath)path
{
  YourTableClass *cell=[tableView cellForRowAtIndexPath:path];
  if(cell.isTaped)
    return increasedHeight;
  else
    return defaultHeight;
}