Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Value of type 'MagnificationGesture' has no member 'onChanged'

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading