代码之家  ›  专栏  ›  技术社区  ›  jbiser361

是否有一种方法可以使尾部的滑动动作具有像macOS 11.0中那样的圆角?

  •  1
  • jbiser361  · 技术社区  · 4 年前

    cornerRadius 其他元素具有的属性(UIView、按钮等)。我附上了我所做工作的代码片段。

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)
    -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: nil) { (_, _, completionHandler) in
            print("removed obj: \(self.setArr[indexPath.row])")
    
            self.setArr.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            UserDefaults.standard.setValue(self.setArr, forKey: "sets")
            
            completionHandler(true)
        }
        deleteAction.image = UIImage(systemName: "trash")
        deleteAction.backgroundColor = .systemRed
        
        let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
        return configuration
    }
    

    我正在努力实现的目标:

    What I am trying to accomplish

    0 回复  |  直到 4 年前
        1
  •  0
  •   Jignesh Mayani    4 年前

    步骤1:在单元中添加自定义视图,并将其框架设置为单元外部

    deleteView.frame = CGRect(x: cell.width, y: 0, width: 50, height: 50) // Here you need to set delete view outside the cell frame
    

    第2步:您需要在单元格中添加点击手势。这是我用的 https://github.com/SwipeCellKit/SwipeCellKit

    //Here You can use your custom logic for cell gesture animation
    
    var swipeIndex: Int = 0 // Here this is global variable
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
    {
        if orientation == .right
        {
            if self.swipeIndex != indexPath.row
            {
                self.hideSwipeView(at: self.swipeIndex)
            }
            self.showSwipeView(at: indexPath.row)
        }
        else if orientation == .left
        {
            hideSwipeView(at: indexPath.row)
        }
        return nil
    }
    
    func hideSwipeView(at index:Int)
    {
        if (index < yourArray.count)
        {
            let cell = tableView.cellForRow(at: IndexPath.init(row: index, section: 0)) as! DetailTVCell
    
            UIView.animate(withDuration: 0.1,
                           delay: 0,
                           options: [.curveEaseOut],
                           animations:
                {
                    cell.viewCell.transform = CGAffineTransform(translationX: 0, y: 0) // Here viewCell is Main View inside Cell and all cell's content is inside viewCell
                    cell.deleteView.transform = CGAffineTransform(translationX: 0, y: 0)
            }) { _ in
                cell.btnDelete.isHidden = true
            }
        }
    }
    
    func showSwipeView(at index:Int)
    {
        let cell = tableView.cellForRow(at: IndexPath.init(row: index, section: 0)) as! DetailTVCell
        
        cell.btnDelete.isHidden = false
        
        UIView.animate(withDuration: 0.1,
                       delay: 0,
                       options: [.curveEaseOut],
                       animations:
            {
                cell.viewCell.transform = CGAffineTransform(translationX: -100, y: 0)
                cell.deleteView.transform = CGAffineTransform(translationX: -100, y: 0)
        }) { _ in
            self.swipeIndex = index
         }
       }
    
        2
  •  0
  •   thefaj    3 年前

    尝试将UICollectionViewCompositionLayout与侧栏外观一起使用。(上面的另一个答案不正确。)

    let layout = UICollectionViewCompositionalLayout() { sectionIndex, layoutEnvironment in
    var configuration = UICollectionLayoutListConfiguration(appearance: .sidebar)
    configuration.leadingSwipeActionsConfigurationProvider = { indexPath in