有没有办法让拖尾的滑动操作像 macOS 11.0 一样具有圆角?
Posted
技术标签:
【中文标题】有没有办法让拖尾的滑动操作像 macOS 11.0 一样具有圆角?【英文标题】:Is there a way I can make the trailing Swipe Actions have rounded corners like in macOS 11.0? 【发布时间】:2021-01-24 22:19:07 【问题描述】:我正在编写的 ios 应用程序有一种气泡般的感觉,我想让它与整个 UI 保持一致。我唯一无法保持不变的是尾随滑动动作。 macOS Big Sur 有一些类似于我想要实现的东西(附加)。我尝试自定义按钮,但它似乎没有其他元素(UIViews、Buttons 等)具有的 cornerRadius
属性。我附上了我所做的代码 sn-p。
我有什么办法可以完成这样的动作吗?
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
我想要完成的事情:
【问题讨论】:
【参考方案1】:尝试使用带有侧边栏外观的 UICollectionViewCompositionalLayout。 (上面的另一个答案是不正确的。)
let layout = UICollectionViewCompositionalLayout() sectionIndex, layoutEnvironment in
var configuration = UICollectionLayoutListConfiguration(appearance: .sidebar)
configuration.leadingSwipeActionsConfigurationProvider = indexPath in
【讨论】:
【参考方案2】:第 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
【讨论】:
以上是关于有没有办法让拖尾的滑动操作像 macOS 11.0 一样具有圆角?的主要内容,如果未能解决你的问题,请参考以下文章
macOS Sierra(10.12.6), odoo(11.0), Python(3.5.4)配置