UIImageView 在 UITableViewCell 内旋转动画

Sid*_*kan 3 uitableview uiimageview cgaffinetransform ios swift

我试图在选择时旋转 UIImage 视图row 0。在选择时,该部分需要重新加载以添加另外两个单元格。这是动画无法工作的地方。imageview 只是转换到新位置而不执行动画。

      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)
    }
Run Code Online (Sandbox Code Playgroud)

此外,行 = 0 处的特定单元格,内容需要更新。这是一个示例项目:

Rei*_*ian 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

    }
Run Code Online (Sandbox Code Playgroud)

将此代码用于您的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
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

希望这可以帮助