Siddharthan Asokan
Siddharthan Asokan

Reputation: 4441

UIImageView Rotate Animation inside UITableViewCell

I'm trying to rotate an UIImage view on selecting row 0. On selection that section needs to reload to add two more cells. This is where the animation fails to work. The imageview just transforms to the new position without performing the animation.

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
            if indexPath.row == 0 {
                print(cell)
            }

            if displayCell {


                UIView.animate(withDuration:0.3, animations: {
                    cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
                })



                if indexPath.row != 0 {
                    swap(&indexArr[indexPath.row], &indexArr[0])
                    print(indexArr)
                }
            } else {

                UIView.animate(withDuration: 0.3, animations: {
                    cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
                })

            }

            displayCell = !displayCell

            tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
    }

Also that particular cell at row = 0, the content needs to be updated. Here is a sample project:

Upvotes: 4

Views: 1901

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

You need reload your UITableView sections after your animations is over

You also need modify your cellForRow method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
        switch indexPath.section {
        case 0:
            cell.rotateButtonImageView.isHidden =  indexPath.row != 0 || indexArr.count <= 2
            if(indexPath.row == 0)
            {
                if displayCell{
                    cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
                }else{
                    cell.rotateButtonImageView.transform = .identity
                }
            }
            break
        default :
            cell.rotateButtonImageView.isHidden = true
            break

        }
        cell.indexLabel.text = indexArr[indexPath.row].0

        return cell

    }

Use this code for your didSelectRowAt method

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
    if indexPath.row == 0 {
        print(cell)
    }

    if displayCell {

        cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
        UIView.animate(withDuration: 0.3, animations: {
            cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
        }, completion: { (finished) in
            tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
        })

        if indexPath.row != 0 {
            swap(&indexArr[indexPath.row], &indexArr[0])
            print(indexArr)
        }
    } else {

        cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
        UIView.animate(withDuration: 0.3, animations: {
            cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
        }, completion: { (finished) in
            tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
        })

    }

    displayCell = !displayCell
}

enter image description here

Hope this helps

Upvotes: 6

Related Questions