【问题标题】:Xcode Compiler error: The compiler is unable to type-check this expression in reasonable time (Xcode 12.0 SwiftUI)Xcode 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查(Xcode 12.0 SwiftUI)
【发布时间】:2020-11-09 09:15:13
【问题描述】:

(Xcode 12.0,SwiftUI) 这段代码为我工作了将近 6 个月,没有任何问题,我昨天刚收到这个错误,没有进行任何编辑。我尝试了很多来清理代码并使其更短,但对我没有任何效果。

GeometryReader { (geometry: GeometryProxy) in

  ForEach(0..<5) { index in

    Group {

      Circle()
        .foregroundColor(Color("GreyOne"))
        .frame(width: geometry.size.width / 5, height: geometry.size.height / 5)

        .scaleEffect(!self.isAnimating ? 1 - CGFloat(index) / 5 : 0.2 + CGFloat(index) / 5)

        .offset(y: geometry.size.width / 10 - geometry.size.height / 2)

      }.frame(width: geometry.size.width, height: geometry.size.height)

        .rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))

        .animation(Animation
            
            .timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)

          .repeatForever(autoreverses: false))

    }

}.aspectRatio(1, contentMode: .fit)

    .onAppear {

      self.isAnimating = true

    }

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您可以将View 的部分内容移出到单独的私有函数中,然后从body 调用这些函数。您的代码中有几个编译器错误,例如未将x 传递给offset 函数或未将所有Ints 转换为CGFloat。一旦你将bodyView 分开,真正的错误就会浮出水面。

    这是您的代码的编译版本,使用 2 个私有函数来构建您的 Viewbody 的一部分。

    struct YourView: View {
        @State private var isAnimating = false
    
        var body: some View {
            GeometryReader { geometry in
                ForEach(0..<5) { index in
                    Group {
                        circle(geometry: geometry, index: index)
                    }
                    .frame(width: geometry.size.width, height: geometry.size.height)
                    .rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))
                    .animation(animation(index: index))
                }
            }
            .aspectRatio(1, contentMode: .fit)
            .onAppear {
                self.isAnimating = true
            }
        }
    
        private func animation(index: Int) -> Animation {
            Animation
                .timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)
                .repeatForever(autoreverses: false)
        }
    
        private func circle(geometry: GeometryProxy, index: Int) -> some View {
            let width = geometry.size.width
            let height = geometry.size.height
            let yOffset = width / 10 - height / 2
            let nonAnimatingScale = 1 - CGFloat(index) / CGFloat(5)
            let animatingScale = 0.2 + CGFloat(index) / CGFloat(5)
            return Circle()
                .foregroundColor(Color("GreyOne"))
                .frame(width: width / 5, height: height / 5)
                .scaleEffect(self.isAnimating ? animatingScale : nonAnimatingScale)
                .offset(x: 0, y: yOffset)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      相关资源
      最近更新 更多