Not sure if I am just doing something wrong or if it is a bug
This code will not compile in Xcode 13.2.1
compile error
Value of type ‘MagnificationGesture’ has no member ‘onChanged’
struct MagnificationGesture: View {
@State private var currentAmount = 0.0
@State private var finalAmount = 1.0
var body: some View {
Text("Hello, World!")
.font(.title)
.padding(25)
.background(.blue)
.cornerRadius(10)
.scaleEffect(finalAmount + currentAmount)
.gesture(
MagnificationGesture()
.onChanged { amount in
currentAmount = amount
}
.onEnded { amount in
finalAmount += currentAmount
currentAmount = 0
}
)
}
}
>Solution :
You’ve named your View MagnificationGesture — that takes over that name in the current namespace (your module). Then, you try to use SwiftUI’s MagnificationGesture inside gesture() and the compiler thinks you’re referring to your MagnificationGesture.
The simplest solution is to rename your View so that there isn’t a collision between the names:
struct MagnificationGestureView: View { //<-- Here
@State private var currentAmount = 0.0
@State private var finalAmount = 1.0
var body: some View {
Text("Hello, World!")
.font(.title)
.padding(25)
.background(.blue)
.cornerRadius(10)
.scaleEffect(finalAmount + currentAmount)
.gesture(
MagnificationGesture()
.onChanged { amount in
currentAmount = amount
}
.onEnded { amount in
finalAmount += currentAmount
currentAmount = 0
}
)
}
}
Technically, you could also solve this by explicitly using the SwiftUI module name (see below), but this is much more confusing later on — the better practice is just to avoid using the same name as existing types from other modules.
SwiftUI.MagnificationGesture()
.onChanged { amount in
currentAmount = amount
}