UIImageView旋转UITableViewCell里面的动画

问题描述:

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

 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项目:

+0

它是什么,是无法精确地工作? – ozgur

+0

我已经更新了问题 –

你需要你的动画后,重新加载您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

希望这有助于

+0

感谢您的输入,但我尝试了这一点,但没有帮助 –

+0

@SiddharthanAsokan我的答案再次更新,我将发布带有结果的gif –

+0

太棒了〜 –