为什么当视图缩放时,方块会消失?

huangapple go评论65阅读模式
英文:

Why does the squares get disappear when the view get scaled?

问题

以下是您要翻译的内容:

以下代码在不引发任何错误的情况下运行。
代码有两个视图,content 视图显示子视图 Squares
当单击按钮时,它会创建新的黑色正方形,然后单击它们时视图会缩放(放大)。
问题在于缩放时黑色正方形消失了。

我尝试了只有一个视图,然后它会与正方形一起缩放,但一旦将代码嵌入为一个单独的视图,正方形就会消失。

我希望父视图与嵌入其中的所有子视图一起缩放,而不会消失。

英文:

The following code runs without throwing any errors.
The code has two views, the content view shows the child view Squares.
It creates new black squares when the button is clicked, then when you click on them the view gets scaled (magnified).
The issue is the black squares disappear when scaling.

I tried it with only one view, then it scales with the squares, but once I embed the code as a seperate view, then the squares gets disappear.

I want the parent view to scale with all the child views embedded in it without disappearing.

import SwiftUI
import Combine

enum ColumnTypes {
   case Square, Circular
}

final class ColumnsArrayObservedMain: ObservableObject {
    @Published var columnsArray : [ColumnsModelMain] = []
    @Published var randomPosition: CGPoint = CGPoint(x: 0, y: 0)
}

struct ColumnsModelMain: Identifiable {
    let id = UUID()  
    @State var columnWidth: Int
    @State var columnLength: Int
    @State var columnHeight: Int
    @State var columnType: ColumnTypes
    @State var columnPosition: CGPoint
    
 }

struct ContentView: View {
    
    @State var scaleby = 2.0
    @State var scalingEnabled = true;
    
    var body: some View {
        VStack (alignment: .center) {
                        
Squares()
                
        }
        .scaleEffect(scaleby)
                     .onTapGesture {
                         if scalingEnabled == true {
                             withAnimation {scaleby *= 2}
                         }
                     }
        .frame(width: 400, height: 400)
    }
}

struct Squares: View {
    
    @ObservedObject private var columnsArrayObservedObjectLocal = ColumnsArrayObservedMain()
    @State private var columnPosition: CGPoint = CGPoint(x: 0, y: 0)
    @State private var recColour: Color = .black
    @State private var dragAmount: CGPoint?
    
    
    
    var body: some View {
        VStack {
            ZStack {
                ForEach(columnsArrayObservedObjectLocal.columnsArray, id: \.id) { column in
                    
                    
                    Rectangle ()
                        .fill (Color.black)
                        .frame(width: 20, height: 20)
                        .position(column.columnPosition)
                    
                }
                
            }
            .frame(width: 200, height: 200, alignment: .center)
            
            
            
            Button ("Append last") {
                
                columnsArrayObservedObjectLocal.columnsArray.append(ColumnsModelMain( columnWidth: 10, columnLength: 100, columnHeight: 10, columnType: ColumnTypes.Square, columnPosition: columnsArrayObservedObjectLocal.randomPosition))
                
                columnsArrayObservedObjectLocal.randomPosition = CGPoint(x: CGFloat.random(in: 1...400), y: CGFloat.random(in: 1...400))
               
            }
        }
        
    }
    
}

答案1

得分: 0

Squares 中使用:

@StateObject private var columnsArrayObservedObjectLocal = ColumnsArrayObservedMain()

而不是 @ObservedObject private ...

它们不会消失,因为使用 @StateObject,根据文档:

"SwiftUI 在声明状态对象的容器的生命周期内仅创建模型对象的新实例一次",而不是在视图更改时重新创建。

英文:

in Squares use:

@StateObject private var columnsArrayObservedObjectLocal = ColumnsArrayObservedMain()

instead of @ObservedObject private ....

They will not disappear, because using @StateObject, from the docs:
SwiftUI creates a new instance of the model object only once during the lifetime of the container that declares the state object., as opposed to be re-created every time the view changes.

huangapple
  • 本文由 发表于 2023年5月6日 20:39:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定