英文:
View does not update without zoom gesture
问题
我是SwiftUI的新手。但我卡住了,不知道为什么。
我的ContentView
由3个视图组成:DrawPlayer
、DrawNextPoints
和Gameboard
。Player
是一个类而不是结构体。我将其定义为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<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
}
}
}
答案1
得分: 0
在你的 ObservableObject
类 Player
内部,你需要使用 @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<Double>
@Published var _movePoints: Array<Array<Double>>
private var _color: SwiftUI.Color
// …
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论