You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 18中onDrag拖拽释放延迟问题求助:求解决方案或官方回复

iOS 18 SwiftUI onDrag拖拽释放延迟问题

问题确认

该问题是iOS 18专属bug,真机与模拟器上均会出现,拖拽释放时存在0.5-1秒的延迟,切换至iOS 17及更早版本后问题完全消失。经代码排查可排除自身实现问题,开发者社区已有零星相关讨论。

官方回复现状

目前苹果尚未发布针对该问题的公开官方回复,但已有大量开发者通过Apple反馈助手提交bug报告,推测苹果内部已收录此问题。建议受影响的开发者也通过Feedback Assistant提交详细报告,附上复现代码、设备型号及iOS版本信息,助力问题修复进程。

临时解决方案

  • 自定义拖拽交互替代系统onDrag:通过UIKit的UIPanGestureRecognizer封装自定义拖拽组件,绕过SwiftUI原生onDrag的延迟问题。核心逻辑为监听手势事件,手动创建拖拽预览视图并管理拖拽状态,释放时直接触发drop逻辑。
  • 简化DropDelegate操作:确保DropDelegate的performDrop方法仅执行核心必要逻辑,避免在该方法中进行耗时操作(如大量数据解析、同步UI更新),可将非必要操作移至后台线程或提前预处理。
  • 等待系统更新:若使用iOS 18 beta版本,可关注后续beta更新;若为正式版,需等待苹果推送系统补丁修复此问题。

复现代码

import SwiftUI
import UniformTypeIdentifiers

struct ContentView: View {
    @State private var draggableText: String = "Move this"

    var body: some View {
        VStack(spacing: 80) {
            DraggableItem(content: $draggableText)
            
            DropZone {
                Text("Place Here!")
                    .font(.headline)
                    .foregroundColor(.white)
            }
            .frame(width: 150, height: 150)
            .background(Color.green)
            .cornerRadius(12)
        }
        .padding()
    }
}

struct DraggableItem: View {
    @Binding var content: String

    var body: some View {
        Text(content)
            .frame(width: 120, height: 120)
            .background(Color.red)
            .foregroundColor(.white)
            .cornerRadius(8)
            .onDrag {
                NSItemProvider(object: NSString(string: content))
            }
    }
}

struct DropZone<Content: View>: View {
    var content: () -> Content

    var body: some View {
        ZStack {
            content()
        }
        .onDrop(of: [UTType.text], delegate: DropHandler())
    }
}

struct DropHandler: DropDelegate {
    func performDrop(info: DropInfo) -> Bool {
        // Add logic to handle the drop
        print("Item dropped!")
        return true
    }
}

#Preview {
    ContentView()
}

内容的提问来源于stack exchange,提问作者Timothy Myers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 12:33:25