WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Input with pencil allowed, input with finger deactivated

Forums > SwiftUI

I have programmed a SwiftUI app to draw paths on the iPad with the Apple Pencil. I use the following structure for this:

...

DrawingView(pencilPoints: $pencilPointArray)

.gesture(DragGesture(minimumDistance: 0)

.onChanged({ value in

code

})

.onEnded({ _ in

code

})

...

struct DrawingView: UIViewRepresentable {

@Binding var pencilPoints: [CGPoint]

code

}

How can I prevent input with the finger? Only input with the Pencil should be allowed.

Many thanks for help in advance Roonster

2      

DrawingView(pencilPoints: $pencilPointArray)
    .gesture(DragGesture(minimumDistance: 0)
    .onChanged({ value in
        // code
    })

    .onEnded({ _ in
        // code
    })

...

struct DrawingView: UIViewRepresentable {
    @Binding var pencilPoints: [CGPoint]
        // code
}

You should look up StrokeGestureRecognizer and the relevant Apple documentation around that, so that you can distinguish between a finger and the Apple Pencil - try this link to Apple Developer.

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.