简体   繁体   English

SwiftUI 性能问题,iOS 14.2 中的 UI 滞后

[英]SwiftUI Performance Issues, UI Lagging in iOS 14.2

I am working on an app in SwiftUI, prior to iOS 14.2 UI updates were smooth as butter, But from iOS 14.2, UI updates with a large amount of data or complex data seem to be very laggy (for example using the LazyVGrid or LazyHGrid with a medium amount of data for more than 20 elements in the pageTabView makes the UI updates too slow.我正在 SwiftUI 中开发一个应用程序,在 iOS 14.2 之前,UI 更新非常顺利,但是从 iOS 14.2 开始,具有大量数据或复杂数据的 UI 更新似乎非常滞后(例如使用 LazyVGrid 或 LazyHGrid pageTabView 中超过 20 个元素的中等数据量导致 UI 更新太慢。

Here is my code for GridView这是我的 GridView 代码

 var body: some View {
    
    ScrollView {
        VStack {
            LazyVGrid(columns: columns, spacing: 10.0) {
                ForEach(self.favViewModel.favourites.indices, id: \.self) { fav in
                    createGridCellView(fav: fav)
                        .onLongPressGesture {
                        self.selectedItem = fav
                        self.hasSelectedDropDown = true
                    }
                        .alert(isPresented: self.$showDeleteAlert, content: { () -> Alert in
                            Alert(title: Text("Are you sure you want to delete this?"), message: Text("There is no undo"), primaryButton: .destructive(Text("Delete")) {
                                self.favViewModel.send(action: .deleteFavourite(item: selectedItem))
                            }, secondaryButton: .cancel())
                        })
                }
            }
            
            Color.clear.padding(.bottom, 100)
        }
        
        
        .actionSheet(isPresented: self.$hasSelectedDropDown, content: { () -> ActionSheet in
            ActionSheet(title: Text("Delete Favourite"), buttons: [.destructive(Text("Delete"), action: {
                self.showDeleteAlert = true
            }),.cancel()])
        })
        
    }
    .padding(.top, 10.0)
    .padding(.bottom, 30.0)
    
}

Grid Cell View网格单元视图

struct FavouriteGridCell: View {
@Binding var name: String
@State var icon: String
var handler: () -> Void



func createTransparentCircle() -> some View {
    Circle()
        .frame(width: 80, height: 80)
        .shadow(radius: 1.0)
        .foregroundColor(Color.clear)
        .background(BlurView(style: .light).cornerRadius(50.0).brightness(-0.1))
}

func createRectangleView() -> some View {
    Rectangle()
        .fill(Color.white.opacity(0.3))
        .frame(height: 20.0)
        .cornerRadius(12.5)
}

var body: some View {
    VStack {
        ZStack {
            createTransparentCircle()
            Image(icon)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 50, height: 50)
            
        }.onTapGesture {
            handler()
        }
        
        Text(name)
            .font(Font.custom(Theme.Fonts.Asap_Semibold, size: 14.0))
            .foregroundColor(Color.white)
            .fontWeight(.bold)
            .frame(minWidth: 0,
                   maxWidth: .infinity,
                   minHeight: 0,
                   maxHeight: .infinity,
                   alignment: .center)
            .lineLimit(2)
        
        Color.clear.padding(.bottom, 10.0)
    }
}

} }

Code for PageTabView PageTabView 的代码

VStack {
                    
    TabView(selection: $currentPage){
    FavouriteGridView(favViewModel:       self.favouriteViewModel,soundsViewModel: self.soundsViewModel)
                                .tag(0)
                            
                        }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                    }

Does anyone experience the same issues?有没有人遇到同样的问题?

I am looking forward to your help.我期待着您的帮助。 Thank You谢谢你

问题出在 TabView(PageTabViewStyle) 上,我不知道为什么,但由于 iOS 14.2 在 tabview 中添加数据会使 UI 无响应且滞后,所以现在,我最终删除了苹果提供的 PageTabView 并添加了一个自定义页面样式 TabView 从这里https://swiftwithmajid.com/2019/12/25/building-pager-view-in-swiftui/

my two cents for other people arriving here..我的两分钱给其他到达这里的人..

I got an heavy performance problem.我遇到了一个严重的性能问题。 I DID use LazyVGrid from start, but I hade big performance issues the same.我从一开始就使用 LazyVGrid,但我也遇到了同样的大性能问题。

It was a stupid mistake (due to refactoring.. with few cells it does work fine..)这是一个愚蠢的错误(由于重构..只有很少的单元格它确实可以正常工作..)

I had:我有:

...
  ScrollView {
                 Text("Products:")  
                 ProductsLazyGrid(products: filteredProducts(),
                    
                 }
                }

My mistake was:我的错误是:

INSIDE ProductsLazyGrid I DID write another one:在 ProductsLazyGrid 里面我写了另一个:

   var body: some View {
        ScrollView {
            
            LazyVGrid(columns: calcColumns(), spacing: VSpacing)

.... } ....}

So first thing (before ever firing up Instruments... as per Apple: https://developer.apple.com/documentation/swiftui/creating-performant-scrollable-stacks )所以第一件事(在启动仪器之前......根据Apple: https ://developer.apple.com/documentation/swiftui/creating-performant-scrollable-stacks)

triple check nested Scrollviews.三重检查嵌套的滚动视图。

Hope can help.希望能有所帮助。

Technically I think nesting scrollViews will force SwiftUI to make calculations ANYWAY, zeroing any advantage of laziness.从技术上讲,我认为嵌套滚动视图将迫使 SwiftUI 无论如何都要进行计算,从而将懒惰的任何优势归零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM