2

我正在使用 UIMenu 和 UIAction 设置上下文菜单。当按下“删除”时,我会显示一个带有“确认”和“取消”功能的子菜单。除了 70% 的时间出现的一个视觉故障外,一切正常。单击“删除”时,上下文菜单的背景会增加 1 帧。其他所有操作都可以正常工作。这可能是什么?下面是过程和故障的截图

在点击“删除”之前点击后故障 1 帧之后一切都很好

这是我用来生成上下文菜单的代码。我还使用了许多其他表格视图功能,因此您可以在此处查看整个文件:源代码第 684-805 行是表格视图代码。

func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    if indexPath.row == habits.count { //if its the last cell which is empty
        return nil
    }
    return UIContextMenuConfiguration(identifier: indexPath as NSIndexPath, previewProvider: nil) { suggestedActions in
        let removeCancel = UIAction(title: "Cancel", image: UIImage(systemName: "xmark")) { action in }
        let removeConfirm = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
            self.removeHabit(index: indexPath.row)
        }
        let remove = UIMenu(title: "Delete", image: UIImage(systemName: "trash"), options: .destructive, children: [removeCancel, removeConfirm])
        
        let edit = UIAction(title: "Edit", image: UIImage(systemName: "square.and.pencil")) { action in
            self.openEditHabit(index: indexPath.row)
        }
        let reset = UIAction(title: "Reset today", image: UIImage(systemName: "arrow.counterclockwise")) { action in
            self.resetHabit(index: indexPath.row)
        }
        let reorder = UIAction(title: "Reorder", image: UIImage(systemName: "arrow.up.arrow.down")) { action in
            self.tableView.isEditing = true
            UISelectionFeedbackGenerator().selectionChanged()
        }
        let cheat = UIAction(title: "Fix streak", image: UIImage(systemName: "slider.horizontal.3")) { action in
            //
        }


        var contextMenu = [UIAction]()
        if (self.habits[indexPath.row].doneToday) {
            contextMenu.append(reset)
        }
        contextMenu.append(reorder)
        contextMenu.append(edit)
        let nonDestructive = UIMenu(options: .displayInline, children: contextMenu)
        return UIMenu(children: [nonDestructive, remove, cheat])
    }
}
4

0 回答 0