Кнопка перетаскивания видимой проблемы в SwiftUIIOS

Программируем под IOS
Ответить
Anonymous
 Кнопка перетаскивания видимой проблемы в SwiftUI

Сообщение Anonymous »

Я создал создатель предложений, как здесь (Pure SwiftUI). Перетаскивание и позиционирование кнопок работают нормально. Проблема в том, что
  • При перетаскивании кнопки она не отображается в области предложений и наоборот.
    (Есть 2 области: предложение и Область кнопок)
  • После удаления кнопки она позиционируется без каких-либо проблем.
  • Другие варианты поведения работают должным образом
Кто-нибудь может помогите мне, как решить эту проблему?
struct SwiftUIDraggableButtonView: View {
@State private var buttons: [DraggableButtonModel] = [
DraggableButtonModel(id: 1, text: "me"),
DraggableButtonModel(id: 2, text: "foreign friends."),
DraggableButtonModel(id: 3, text: "to me,"),
DraggableButtonModel(id: 4, text: "to make"),
DraggableButtonModel(id: 5, text: "means"),
DraggableButtonModel(id: 6, text: "because"),
DraggableButtonModel(id: 7, text: "it"),
DraggableButtonModel(id: 8, text: "allows"),
DraggableButtonModel(id: 9, text: "study"),
DraggableButtonModel(id: 10, text: "My foreign language"),
DraggableButtonModel(id: 11, text: "a lot")
]
@State private var sentenceButtons: [DraggableButtonModel] = []

var body: some View {
VStack(spacing: 20) {
// Unified FlowLayout for sentence and default areas
FlowLayout(spacing: 5, items: sentenceButtons) { button in
DraggableButtonNew(
model: button,
isInSentence: true
) { action in
handleButtonAction(action, button: button)
}
.transition(.move(edge: .bottom))
}
.padding()
.frame(height: 200) // Set height for sentence area
.background(Color.gray)
.cornerRadius(8)
.zIndex(0)

// Default button area
FlowLayout(spacing: 10, items: buttons) { button in
DraggableButtonNew(
model: button,
isInSentence: false
) { action in
handleButtonAction(action, button: button)
}
.allowsHitTesting(!button.isDisabled) // Disable interaction for disabled buttons
.transition(.move(edge: .top))
}
.frame(height: 200) // Set height for default button area
.cornerRadius(8)
.zIndex(1)
Spacer()
}
.padding()
}

private func handleButtonAction(_ action: DraggableButtonAction, button: DraggableButtonModel) {
withAnimation {
switch action {
case .tap, .drag:
// Handle button tap: move button between sentence and default areas
if let index = sentenceButtons.firstIndex(where: { $0.id == button.id }) {
// Button is in the sentence area, move it back to default
sentenceButtons.remove(at: index)
if let defaultIndex = buttons.firstIndex(where: { $0.id == button.id }) {
buttons[defaultIndex].isDisabled = false // Re-enable in default area
}
} else if let defaultIndex = buttons.firstIndex(where: { $0.id == button.id }),
!buttons[defaultIndex].isDisabled {
// Button is in default area, move it to sentence
buttons[defaultIndex].isDisabled = true
sentenceButtons.append(button)
}
}
}
}
}

// FlowLayout for wrapping buttons in multiple lines
struct FlowLayout: View
where Data.Element: Identifiable {
let spacing: CGFloat
let items: Data
let content: (Data.Element) -> Content

var body: some View {
var width: CGFloat = 0
var height: CGFloat = 0

return GeometryReader { geometry in
ZStack(alignment: .topLeading) {
ForEach(items) { item in
content(item)
.alignmentGuide(.leading) { d in
if abs(width - d.width) > geometry.size.width {
width = 0
height -= d.height + spacing
}
let result = width
if item.id == items.last?.id {
width = 0
} else {
width -= d.width + spacing
}
return result
}
.alignmentGuide(.top) { _ in
let result = height
if item.id == items.last?.id {
height = 0
}
return result
}
}
}
}
.frame(maxHeight: .infinity, alignment: .topLeading)
}
}
// Draggable Button
struct DraggableButtonNew: View {
let model: DraggableButtonModel
let isInSentence: Bool
let actionHandler: (DraggableButtonAction) -> Void
@State private var offset: CGSize = .zero

var body: some View {
Text(model.text)
.padding(8)
.background(isInSentence ? Color.orange : model.isDisabled ? Color.gray : Color.orange)
.foregroundColor(Color.white)
.cornerRadius(6)
.offset(offset)
.zIndex(offset == .zero ? 0 : 1)
.gesture(
DragGesture()
.onChanged { value in
offset = value.translation
}
.onEnded { _ in
actionHandler(.drag)
offset = .zero
}
)
.onTapGesture {
actionHandler(.tap)
}
}
}

// Button Model
struct DraggableButtonModel: Identifiable, Equatable {
let id: Int
let text: String
var isDisabled: Bool = false
}
enum DraggableButtonAction {
case tap
case drag
}


Подробнее здесь: https://stackoverflow.com/questions/793 ... in-swiftui
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»