在UITableViewCell内实现UIImageView旋转动画

4

我试图在选择第0行时旋转一个UIImage视图。在选择后,该部分需要重新加载以添加两个以上的单元格。这就是动画无法正常工作的地方。图像视图只会转换到新位置,而不执行动画。

      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)
    }

还有那个在第0行的特定单元格,需要更新内容。 这是一个示例项目:


具体是哪一部分没有正常工作? - Ozgur Vatansever
我已经更新了问题。 - Siddharthan Asokan
1个回答

6

在你的动画结束后,你需要重新加载你的UITableView部分。

你还需要修改你的cellForRow方法。

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

    }

使用此代码作为您的didSelectRowAt方法

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
}

这里输入图片描述

希望这有所帮助。


谢谢您的建议,但我已经尝试过了,没有帮助。 - Siddharthan Asokan
@SiddharthanAsokan 我的回答已经更新了,我将发布带有结果的gif。 - Reinier Melian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接