SwiftUI - ограничение перетаскивания прямоугольника внутри вида - PullRequest
1 голос
/ 01 апреля 2020

Ниже приведен код, который я написал для перетаскивания прямоугольника. Я хотел бы ограничить его перетаскивание, чтобы оно находилось в границах данного представления. ie если пользователь пытается переместить прямоугольник за границу вида, он должен оставаться в пределах краев границы. Я пытался использовать GeometryReader, чтобы получить прямоугольник как прямоугольника, так и внешнего вида, но я не могу ограничить перетаскивание. Пожалуйста, помогите.

import SwiftUI

struct DragView: View {
    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero
    @State private var parentRect: CGRect = .zero
    @State private var childRect: CGRect = .zero

    var body: some View {
            VStack {
                Rectangle().frame(width: 100, height: 100, alignment: .top)
                    .foregroundColor(.blue)
                    .offset(x: self.currentPosition.width, y: self.currentPosition.height)
                    .background(GeometryGetter(rect: $childRect))

                .gesture(
                    DragGesture(minimumDistance: 0, coordinateSpace: .global)
                        .onChanged { value in
                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                        }
                        .onEnded { value in
                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                            self.newPosition = self.currentPosition
                        }
                )
            }
            .frame(width: 300, height: 500, alignment: .center)
            .border(Color.black, width: 1)
            .background(GeometryGetter(rect: $parentRect))
    }
}

struct GeometryGetter: View {
    @Binding var rect: CGRect

    var body: some View {
        GeometryReader { geometry in
            Group { () -> AnyView in
                DispatchQueue.main.async {
                    self.rect = geometry.frame(in: .global)
                }
                return AnyView(Color.clear)
            }
        }
    }
}

1 Ответ

1 голос
/ 01 апреля 2020

проверить это:

struct ContentView: View {
    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero
    @State private var parentRect: CGRect = .zero
    @State private var childRect: CGRect = .zero

    func correctPostion() {
        print(self.currentPosition)
        if self.currentPosition.width > 100 {
            self.currentPosition.width = 100
        }
        if self.currentPosition.height > 200 {
            self.currentPosition.height = 200
        }
        if self.currentPosition.width < -100 {
            self.currentPosition.width = -100
        }
        if self.currentPosition.height < -200 {
            self.currentPosition.height = -200
        }
    }

    var body: some View {
            VStack {
                Rectangle().frame(width: 100, height: 100, alignment: .top)
                    .foregroundColor(.blue)
                    .offset(x: self.currentPosition.width, y: self.currentPosition.height)
                    .background(GeometryGetter(rect: $childRect))

                .gesture(
                    DragGesture(minimumDistance: 0, coordinateSpace: .global)
                        .onChanged { value in
                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

                            self.correctPostion()
                        }
                        .onEnded { value in
                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

                            self.correctPostion()

                            self.newPosition = self.currentPosition
                        }
                )
            }
            .frame(width: 300, height: 500, alignment: .center)
            .border(Color.black, width: 1)
            .background(GeometryGetter(rect: $parentRect))
    }
}

struct GeometryGetter: View {
    @Binding var rect: CGRect

    var body: some View {
        GeometryReader { geometry in
            Group { () -> AnyView in
                DispatchQueue.main.async {
                    self.rect = geometry.frame(in: .global)
                }
                return AnyView(Color.clear)
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...