Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell animate resize when user touchdown or tap Cell

I want to animate a UITableViewCell like in the following image (The cell gets resized smaller) before detailedViewController is show. I have tried it from last two days and tried other all the answers I found for similar questions.

I want the cell to animate/resize as soon as user put his finger on the UITableViewCell.

enter image description here

The UITableViewCell is a custom cell. I have tried setting a new frame to cell.contentView in UITableViewCell subclass's setSelected:(BOOL)selected animated:(BOOL)animated method. The UITableViewCell didn't resize as I expected.

What I have also observed is this method is called when user lifts his finger up from the UITableViewCell. Actually I want it to happen as soon as user taps the cell. And if the cell is not selected and user moves to some other cell, cell should get resized back to normal size.

Then I did some searching on receiving touch events from UITableViewCell. And I tried to add a UILongPressGestureRecognizer and UITapGestureRecognizer and in the selector I tried to change cell.contentView frame like this

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

But the selected cell is only flashing(cell background) it is not resizing as I expected.

Any suggestions would be really helpful. Thanks in advance

like image 539
Priyatham51 Avatar asked Oct 31 '14 07:10

Priyatham51


1 Answers

Here is an simpler way to da cell animation on tap in 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
    }
}
like image 163
Serge Avatar answered Sep 29 '22 14:09

Serge