2017-08-16 80 views
2

我想旋轉選擇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

我已經更新了問題 –

回答

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

希望這有助於

+0

感謝您的輸入,但我嘗試了這一點,但沒有幫助 –

+0

@SiddharthanAsokan我的答案再次更新,我將發佈帶有結果的gif –

+0

太棒了〜 –