HomeiOS Developmentios - Add Multitouch Assist in SwiftUI to DragGesture

ios – Add Multitouch Assist in SwiftUI to DragGesture


I’m at present engaged on a SwiftUI mission the place I have to implement true multitouch assist on a view that consists of a number of buttons. The prevailing implementation helps dragging throughout these buttons to replace an activeButtonIndex state, highlighting the button at present being dragged over. Nonetheless, it at present solely helps single-touch interactions. I need to improve this to assist multitouch, the place a number of buttons might be activated concurrently if a number of fingers are used.

I wish to preserve all the present performance. Particularly, I’m in search of a SwiftUI-only resolution, with out resorting to UIKit interop.

I attempted referencing Apple’s developer documentation article Composing SwiftUI gestures to no avail.

Right here’s the simplified model of my present code:

import SwiftUI

struct ContentView: View {
    @State personal var activeButtonIndex: Int? = nil
    @State personal var buttonFrames: [CGRect] = Array(repeating: .zero, depend: 11)
    personal let buttonCount = 11
    
    var physique: some View {
        VStack {
            activeButtonView
            buttonsView
        }
        .gesture(dragGesture)
    }
    
    personal var activeButtonView: some View {
        Textual content("Lively Button (activeButtonIndex.map { "n($0 + 1)" } ?? "nNone")")
            .multilineTextAlignment(.heart)
            .padding()
    }
    
    personal var buttonsView: some View {
        ScrollView(.horizontal) {
            LazyHStack {
                ForEach(0 ..< buttonCount, id: .self) { index in
                    Button(motion: {}) {
                        EmptyView()
                    }
                    .body(width: 70, peak: 210)
                    .background(backgroundForButton(at: index))
                    .overlay(GeometryReader { geometry in
                        Colour.clear.onAppear {
                            buttonFrames[index] = geometry.body(in: .international)
                        }
                    })
                }
            }
        }
    }
    
    personal func backgroundForButton(at index: Int) -> Colour {
        activeButtonIndex == index ? .inexperienced : .blue
    }
    
    personal var dragGesture: some Gesture {
        DragGesture(minimumDistance: 0, coordinateSpace: .international)
            .onChanged { worth in
                updateActiveButton(with: worth.location)
            }
            .onEnded { _ in
                activeButtonIndex = nil
            }
    }
    
    personal func updateActiveButton(with location: CGPoint) {
        for (index, body) in buttonFrames.enumerated() {
            if body.accommodates(location) {
                activeButtonIndex = index
                return
            }
        }
        activeButtonIndex = nil
    }
}

Has anybody applied one thing related or can information me on learn how to strategy including multitouch assist on this context? Any recommendation or examples could be significantly appreciated.

interactive demo of drag gesture

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments