从 iOS 9 开始,UICollectionView 通过beginInteractiveMovement及其同级方法支持交互式移动。从iOS 11开始,它还支持拖放,这似乎也支持移动。在 iOS 13 beta 4 中,拖放手势已与UIContextMenuInteraction动画相结合(Twitter 链接),因此您可以从上下文菜单过渡到拖动操作,但通过交互式移动 API 重新排序时情况并非如此。
在集合视图中使用拖放来实现项目的交互式移动是否更好?
我正在使用UIContextMenuInteraction以下内容显示上下文菜单UICollectionView:
func collectiovnView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { _ in
let deleteAction = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
self.deleteItem(at: indexPath)
}
return UIMenu(title: "Actions", children: [deleteAction])
})
}
func deleteItem(at indexPath: IndexPath) {
self.collectionView.performBatchUpdates({
self.items.remove(at: indexPath.item)
self.collectionView.deleteItems(at: [indexPath])
})
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但是当我点击“删除”项目时,会发生一个奇怪的动画,其中已删除的项目留在原地而其他项目正在移动,然后它立即消失。有时我什至在新项目出现之前的几分之一秒内看到一个空白区域或一个随机项目。
如果我collectionView.deleteItems()在上下文菜单未显示时调用,则删除动画按预期工作。
iOS 13.4 更新(2020 年 3 月):
UIPointerInteraction将鼠标悬停在链接上时也会发生这种情况。
我有一个视图,当用户长按链接时,显示富文本并显示 iOS 13 上下文菜单。当用户开始长按时,我希望能够仅突出显示链接而不是整个视图。
为此,我提供了一个UITargetedPreview对象,其中包含UIPreviewParameters要在视图中CGRect突出显示的每行的 s 。UIContextMenuInteractionDelegate这正确地突出显示了链接,但也产生了隐藏视图其余部分的副作用。
这张图说明了这个问题:
请注意,虽然链接正确突出显示,但当长按链接然后释放时,视图的其余部分会闪烁。
将此与 Apple 自己的 Notes.app 中的行为进行比较:
请注意,长按链接时视图的其余部分不会消失。这在 Apple 的其他应用程序(例如 Safari)中也能按预期工作。
我UITargetedPreview通过以下方式向交互委托提供 s:
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, previewForHighlightingMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard let range = configuration.identifier as? NSRange else { return nil }
let lineRects: [NSValue] = // calculate appropriate rects for the range of text
let parameters = UIPreviewParameters(textLineRects: lineRects) …Run Code Online (Sandbox Code Playgroud) uikit ios swift uicontextmenuinteraction uipointerinteraction
ios swift uicontextmenuinteraction uicontextmenuconfiguration
假设您有一个图像的上下文菜单,长按时会弹出该图像。如何使弹出窗口更大,但保持相同的尺寸?
ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
let share = UIAction(title: "", image: UIImage(systemName: "")) { _ in
// share code
}
return UIMenu(title: "", children: [share])
}
}
Run Code Online (Sandbox Code Playgroud)
override func awakeFromNib() {
super.awakeFromNib()
immy.isUserInteractionEnabled = true
immy.addInteraction(UIContextMenuInteraction(delegate: self))
}
Run Code Online (Sandbox Code Playgroud) contextmenu uiimageview firebase swift uicontextmenuinteraction