Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

ios - Is there a way I can make the trailing Swipe Actions have rounded corners like in macOS 11.0?

The iOS application I am writing has a bubble like feel and I want to make it constant with the whole UI. The only thing that I am not able to get constant is the trailing swipe actions. macOS Big Sur has something like what I am trying to achieve (attached). I have tried customizing the buttons, but it does not seem to have the cornerRadius attribute other elements have (UIViews, Buttons, etc). I attached a code snippet of what I did.

Is there any way for me to accomplish such actions?

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:

What I am trying to accomplish

question from:https://stackoverflow.com/questions/65876602/is-there-a-way-i-can-make-the-trailing-swipe-actions-have-rounded-corners-like-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Step 1 : Add your custom view in cell and set its frame as outside the cell

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

Step 2 : You need to add tap gesture to you cell. Here I'm using https://github.com/SwipeCellKit/SwipeCellKit 's delegate method for handling swipe animations.

//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
     }
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...