SwiftUI-使用上下文菜单删除列表中的行-UI故障 [英] SwiftUI - delete row in list with context menu - UI glitch

查看:64
本文介绍了SwiftUI-使用上下文菜单删除列表中的行-UI故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SwiftUI视图中使用 List 显示了一系列项目.我累了要添加 contextMenu 来删除 List 中的单个项目.结果如下.

I've a array of items displayed using List in my SwiftUI View. I tired to add a contextMenu to delete individual items in the List. The following is the result.

动画不是预期的.该行闪烁,然后移动下一个.如何设置 animation.right 或类似的设置,以使UI没有毛刺,并且看起来像在 onDelete 上发生的默认行为.

The animation is not what is expected. The row blinks before moving the next one. How to set animation.right or something like that so that there is no UI glitch and looks like the default behavior which happens at onDelete.

PS:我不能使用 onDelete ,因为在我的应用中,左右滑动还有其他功能.

PS: I can't use onDelete because, in my app, swiping right and left has other functions.

这是代码.


struct ListDelete: View {
    
    @State var cars = ["Tesla", "Mercedes", "Audi", "Tata", "Jaguar"]
    
    var body: some View {
        List(cars, id: \.self) { car in
            Text(car).contextMenu {
                Button(action: {
                    if let index = self.cars.firstIndex(of: car) {
//                        self.cars.remove(at: index)
                        self.cars.remove(atOffsets: [index])
                    }
                }, label: {
                    HStack {
                        Text("Delete")
                        Spacer()
                        Image(systemName: "trash")
                    }
                })
            }
        }
    }
}

用于从数组中删除项目的两种方法导致了相同的行为.

The two approaches used to remove the items from the array, resulted in this same behavior.

推荐答案

SwiftUI存在问题,希望Apple在下一个主要版本中对其进行修复.目前,您可以通过在上下文按钮操作中执行操作之前增加一小段延迟来解决该问题:

It’s an issue with SwiftUI, hopefully Apple fix it in the next major release. For now you can solve the issue by adding a small delay before actions are performed in your context button action:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.7){
    //delete row
}

这篇关于SwiftUI-使用上下文菜单删除列表中的行-UI故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆