UIImageView在UITableViewCell内旋转动画

时间:2017-08-16 15:31:47

标签: ios swift uitableview uiimageview cgaffinetransform

我试图在选择row 0时旋转UIImage视图。在选择该部分需要重新加载以添加另外两个单元格。这是动画无法工作的地方。 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)
    }

同样在行= 0的特定单元格中,内容需要更新。 这是一个sample项目:

1 个答案:

答案 0 :(得分:2)

您需要在动画结束后重新加载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
}

enter image description here

希望这有帮助