视图不会在没有缩放手势的情况下更新。

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

View does not update without zoom gesture

问题

我是SwiftUI的新手。但我卡住了,不知道为什么。

我的ContentView由3个视图组成:DrawPlayerDrawNextPointsGameboardPlayer是一个类而不是结构体。我将其定义为ObservableObject,因为我将更改其属性。使用我的DrawNextPoint,我绘制一个圆圈并添加了.onTabGesture。所有这些都有效,但我必须使用MagnificationGesture来使我的ContentView获得更新并显示点击的圆圈。
如果我不进行缩放,什么都不会更新。

谢谢你的帮助。

这是我的ContentView:

import SwiftUI

struct ContentView: View {
    
    @State private var scale: CGFloat = 1.0
        
    @StateObject var player1 = Player(actualPoint: Gameboard().startPoint1, color: .red)
    
    var body: some View {
        ScrollView ([.horizontal, .vertical]) {
            ZStack {
                VStack {
                    Gameboard()
                }
                var _ = print(player1.actualPoint)
                var _ = print(player1.movePoints)
                ForEach(player1.actualPoint, id:\.self) { actualPoint in
                    DrawNextPoints(player: player1)
                }
                
                ForEach(player1.movePoints, id:\.self) { movePoints in
                    DrawPlayer(player: player1)
                }
                
            }
            .position(CGPoint(x:1000, y:1000))
            .scaleEffect(scale)
            .gesture(MagnificationGesture()
                .onChanged { scale in
                    self.scale = scale.magnitude
                }
            )
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

以及我的Player类:

import SwiftUI

class Player: ObservableObject {
    
    private var _speedX: Double = 0.0
    private var _speedY: Double = 1.0
    var _actualPoint: Array<Double>
    var _movePoints: Array<Array<Double>>
    private var _color: SwiftUI.Color
    
    public init(actualPoint: Array<Double>, color: SwiftUI.Color) {
        self._actualPoint = actualPoint
        self._color = color
        self._movePoints = [actualPoint]
    }
    
    public var speedX:Double {
        get{
            return _speedX
        }
        set{
            _speedX = newValue
        }
    }
    
    public var speedY:Double {
        get{
            return self._speedY
        }
        set{
            self._speedY = newValue
        }
    }
    
    public var actualPoint: Array<Double> {
        get{
            return self._actualPoint
        }
        set{
            self._actualPoint = newValue
        }
        
    }
    
    public var movePoints: Array<Array<Double>> {
        get{
            return self._movePoints
        }
        set{
            self._movePoints = newValue
        }
    }
    
    public var color: SwiftUI.Color {
        get{
            return self._color
        }
    }
    
}

希望这可以帮助你。如果你有任何其他问题,请随时提问。

英文:

I am new to SwiftUI. But i got stuck and i don't know why.

My ContentView is combined out of 3 views: DrawPlayer, DrawNextPoints, Gameboard. Player is a class not a struct. I have defined it as an ObservableObject, because i will change its properties. With my DrawNextPoint i draw a circle and add an .onTabGesture. All of this works, but i have to make use of the MagnificationGesture so my ContentView gets an update and displays the clicked circles.
If i don‘t zoom, nothing updates.

Thanks for your help.

Here is my ContentView:

import SwiftUI

struct ContentView: View {
    
    @State private var scale: CGFloat = 1.0
        
    @StateObject var player1 = Player(actualPoint: Gameboard().startPoint1, color: .red)
    
    var body: some View {
        ScrollView ([.horizontal, .vertical]) {
            ZStack {
                VStack {
                    Gameboard()
                }
                var _ = print(player1.actualPoint)
                var _ = print(player1.movePoints)
                ForEach(player1.actualPoint, id:\.self) { actualPoint in
                    DrawNextPoints(player: player1)
                }
                
                ForEach(player1.movePoints, id:\.self) { movePoints in
                    DrawPlayer(player: player1)
                }
                
            }
            .position(CGPoint(x:1000, y:1000))
            .scaleEffect(scale)
            .gesture(MagnificationGesture()
                .onChanged { scale in
                    self.scale = scale.magnitude
                }
            )
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

And my Player class:

import SwiftUI

class Player: ObservableObject {
    
    private var _speedX: Double = 0.0
    private var _speedY: Double = 1.0
    var _actualPoint: Array&lt;Double&gt;
    var _movePoints: Array&lt;Array&lt;Double&gt;&gt;
    private var _color: SwiftUI.Color
    
    public init(actualPoint: Array&lt;Double&gt;, color: SwiftUI.Color) {
        self._actualPoint = actualPoint
        self._color = color
        self._movePoints = [actualPoint]
    }
    
    public var speedX:Double {
        get{
            return _speedX
        }
        set{
            _speedX = newValue
        }
    }
    
    public var speedY:Double {
        get{
            return self._speedY
        }
        set{
            self._speedY = newValue
        }
    }
    
    public var actualPoint: Array&lt;Double&gt; {
        get{
            return self._actualPoint
        }
        set{
            self._actualPoint = newValue
        }
        
    }
    
    public var movePoints: Array&lt;Array&lt;Double&gt;&gt; {
        get{
            return self._movePoints
        }
        set{
            self._movePoints = newValue
        }
    }
    
    public var color: SwiftUI.Color {
        get{
            return self._color
        }
    }
    
}

答案1

得分: 0

在你的 ObservableObjectPlayer 内部,你需要使用 @Published 属性包装器:它会通知视图观察到的对象将要发生变化。

在你的类中这样做:

class Player: ObservableObject {
    
    private var _speedX: Double = 0.0
    private var _speedY: Double = 1.0
    @Published var _actualPoint: [Double]
    @Published var _movePoints: [[Double]]
    private var _color: SwiftUI.Color

    // …
    
}
英文:

Inside your ObservableObject class Player, you need to use the @Published property wrapper: it notifies the views that the observed object will change.

In your class do this:


class Player: ObservableObject {
    
    private var _speedX: Double = 0.0
    private var _speedY: Double = 1.0
    @Published var _actualPoint: Array&lt;Double&gt;
    @Published var _movePoints: Array&lt;Array&lt;Double&gt;&gt;
    private var _color: SwiftUI.Color

// …

}

huangapple
  • 本文由 发表于 2023年7月27日 22:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780724.html
匿名

发表评论

匿名网友

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

确定