首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIKit Swift:阴影故障,上下文菜单

UIKit Swift:阴影故障,上下文菜单
EN

Stack Overflow用户
提问于 2022-07-25 16:24:09
回答 1查看 88关注 0票数 1

我有一个带有阴影和上下文菜单的矩形。当我关闭这个上下文菜单时,矩形的阴影会延迟出现(~0.5秒)。完整矩形的阴影以及内部元素的阴影。我不知道我做错了什么

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
    
    if collectionView == foodOnTheShelf {
        
        let config = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in

            let edit = UIAction(title: "Изменить", image: UIImage(systemName: "pencil"), identifier: nil, discoverabilityTitle: nil, state: .off) { _ in
                self.alert.showAlert(viewController: self,
                                     image:  UIImage(named: test[indexPath.row].name)!,
                                     food: test[indexPath.row],
                                     picker: self.picker,
                                     consumePicker: self.consumePicker,
                                     unit: self.unit,
                                     currentWeigt: test[indexPath.row].weight,
                                     currentProductDate: test[indexPath.row].productionDate,
                                     currentExperationDate: test[indexPath.row].expirationDate,
                                     searchController: nil)
            }
            
            let delete = UIAction(title: "Удалить", image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
                test.remove(at: indexPath.row)
                self.viewDidAppear(true)
            }
            return UIMenu(title: "", image: nil, identifier: nil, options: UIMenu.Options.displayInline , children: [edit, delete])
        }
        return config
    }
 return nil
    

}
EN

回答 1

Stack Overflow用户

发布于 2022-09-21 05:41:48

基于这个答案,您可以向盘过渡视图添加阴影,这是在上下文菜单中断结束后显示的临时视图。

问题是这个转换视图是一个私有类(_UIContextMenuPlatterTransitionView),所以如果使用它,应用程序将被拒绝。

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, willEndContextMenuInteraction configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
    guard let platter = platterTransitionView() else { return }
    addShadow(to: platter.layer) // add a shadow the same way you do for your cells
}

private func platterTransitionView() -> UIView? {
    guard let container = self.view.window?.subviews.filter({ type(of: $0) == NSClassFromString("_UIContextMenuContainerView") }).first else { return nil }
    return container.subviews.filter({ type(of: $0) == NSClassFromString("_UIContextMenuPlatterTransitionView") }).first
}

您也可以尝试在不检查其类的情况下访问转换视图,但这种方式并不安全/动态:

代码语言:javascript
复制
let platter = self.view.window?.subviews[2].subviews[1]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73112474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档