How to make a button draggable/movable with SwiftUI?

狂风中的少年 提交于 2020-12-12 15:52:12

问题


I am trying to make a Button Movable with SwiftUI. From what it looks like this should work. I have tried putting the Button with Text inside another ZStack and for a second it was working but as soon as I released the button, the dragging stopped and I couldn't drag anymore. I noticed that the tap was remaining in the center despite the button had moved. Also the dragging looked buggy.

 struct CircleButton: View {
@State private var dragAmount = CGSize.zero
var body: some View {
    ZStack {
        Button(action: performAction){
            ZStack {
                Circle()
                    .foregroundColor(.blue)
                    .frame(width: 100, height: 100)
                Text("Move me")
                    .foregroundColor(.white)
                    .font(.system(.caption, design: .serif))
            }
        }
        .animation(.default)
        .offset(self.dragAmount)
    }
    .gesture(
        DragGesture()
            .onChanged { self.dragAmount = $0.translation})
}

func performAction(){
    print("button pressed")
 }
}

I tried this:

struct CircleButton: View {
@State private var dragAmount = CGSize.zero
var body: some View {
    ZStack {
        ZStack {
            Button(action: performAction){
                Circle()
                    .foregroundColor(.blue)
                    .frame(width: 100, height: 100)
            }
            Text("Tap me")
        }
        .offset(self.dragAmount)
        .animation(.default)
    }
    .gesture(
        DragGesture()
            .onChanged{ self.dragAmount = $0.translation})
}

func performAction(){
    print("button pressed")
 }
}

回答1:


Here is a demo of possible approach. Tested & works with Xcode 11.4 / iOS 13.4

See also notes inline.

struct CircleButton: View {
    @State private var dragAmount: CGPoint?
    var body: some View {
        GeometryReader { gp in // just to center initial position
            ZStack {
                Button(action: self.performAction) {
                    ZStack {
                        Circle()
                            .foregroundColor(.blue)
                            .frame(width: 100, height: 100)
                        Text("Move me")
                            .foregroundColor(.white)
                            .font(.system(.caption, design: .serif))
                    }
                }
                .animation(.default)
                .position(self.dragAmount ?? CGPoint(x: gp.size.width / 2, y: gp.size.height / 2))
                .highPriorityGesture(  // << to do no action on drag !!
                    DragGesture()
                        .onChanged { self.dragAmount = $0.location})
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity) // full space
        }
    }

    func performAction() {
        print("button pressed")
    }
}



回答2:


how about something different and concise:

import SwiftUI

struct ContentView: View {
var body: some View {
    CircleButton()
}
}

struct CircleButton: View {

@State private var pos = CGPoint(x:222,y:222) // just for testing

var body: some View {
    theButton.position(self.pos).highPriorityGesture(self.drag)
}

var theButton: some View {
    ZStack {
        Circle()
            .foregroundColor(.blue)
            .frame(width: 100, height: 100)
            .onTapGesture { self.performAction() }
        Text("Tap me")
            .foregroundColor(.white)
            .font(.system(.caption, design: .serif))
    }
}

func performAction(){
    print("button pressed")
}

var drag: some Gesture {
    DragGesture().onChanged { value in self.pos = CGPoint(x: value.location.x, y: value.location.y)}
}
}



回答3:


I found a simple solution here by ohayon . Just a ViewModifier and an extension:

struct DraggablePita: View {
  var body: some View {
    Image(uiImage: UIImage(named: "pita.png")!)
      .draggable() // Add the new, custom modifier to make this draggable
  }
}

// Handle dragging
struct DraggableView: ViewModifier {
  @State var offset = CGPoint(x: 0, y: 0)

  func body(content: Content) -> some View {
    content
      .gesture(DragGesture(minimumDistance: 0)
        .onChanged { value in
          self.offset.x += value.location.x - value.startLocation.x
          self.offset.y += value.location.y - value.startLocation.y
      })
      .offset(x: offset.x, y: offset.y)
  }
}

// Wrap `draggable()` in a View extension to have a clean call site
extension View {
  func draggable() -> some View {
    return modifier(DraggableView())
  }
}


来源:https://stackoverflow.com/questions/61264499/how-to-make-a-button-draggable-movable-with-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!