Siddharthan Asokan Siddharthan Asokan - 6 months ago 51
iOS Question

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:

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download