Skip to main content

How to stop system gestures from interfering with your own

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to stop system gestures from interfering with your own 관련

SwiftUI by Example

Back to Home

How to stop system gestures from interfering with your own | SwiftUI by Example

How to stop system gestures from interfering with your own

Updated for Xcode 15

New in iOS 16

SwiftUI's defersSystemGestures() modifier lets us request that our gestures take precedence over the system's own built-in gestures. This is important in various places, such as games where the user might be swiping around a lot, or when you place your own gestures at the screen edges.

As an example, you might be using a drag gesture to let the user control the value of some input – perhaps they are getting fine control over a color, maybe they are working with audio such as a theremin, or maybe it's a game where they are swiping to move their character around. Here we could use defersSystemGestures() like this:

struct ContentView: View {
    @State private var input = 0.0

    var body: some View {
        Text("Current value: \(input)")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .contentShape(Rectangle())
            .gesture(
                DragGesture().onChanged { value in
                    input = value.location.y - value.startLocation.y
                }
            )
            .defersSystemGestures(on: .vertical)
    }
}

Download this as an Xcode projectopen in new window

On iOS that does three distinct things:

  1. If the user pulls down from the top, rather than Control Center appearing immediately they'll see a little tab that needs to be pulled again – it's much harder for them to active Control Center by accident.
  2. The home indicator will fade to a lower opacity, and if the user drags directly on that faded home indicator then it will fade back in. They can then swipe up again to get to the task switcher or home screen.
  3. If the user swipes up from the bottom but to either side of the home indicator, our drag gesture will happen instead.
Similar solutions…
How to make two gestures recognize at the same time using simultaneousGesture() | SwiftUI by Example

How to make two gestures recognize at the same time using simultaneousGesture()
How to let users share content using the system share sheet | SwiftUI by Example

How to let users share content using the system share sheet
How to hide the home indicator and other system UI | SwiftUI by Example

How to hide the home indicator and other system UI
How to detect shake gestures | SwiftUI by Example

How to detect shake gestures
How to read tap and double-tap gestures | SwiftUI by Example

How to read tap and double-tap gestures

이찬희 (MarkiiimarK)
Never Stop Learning.