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

如何检测用户开始拖动ImageAnalysisInteraction中抬起的主体?

解决ImageAnalysisInteraction拖动开始时机检测问题

你可以通过实现ImageAnalysisInteractionDelegate中的interaction(_:didBeginFor:)方法来检测拖动操作的开始时机,这个方法会在交互(包括拖动)正式启动时被调用,和仅用于判断是否允许交互的shouldBeginAt方法不同。

修改你的代码,添加这个代理方法:

class ImageAnalysisViewController: UIViewController, ImageAnalysisInteractionDelegate {
    func interaction(_ interaction: ImageAnalysisInteraction, shouldBeginAt point: CGPoint, for interactionType: ImageAnalysisInteraction.InteractionTypes) -> Bool {
        print("lifted subject")
        return true
    }

    // 新增:检测拖动开始的回调
    func interaction(_ interaction: ImageAnalysisInteraction, didBeginFor interactionType: ImageAnalysisInteraction.InteractionTypes) {
        if interactionType == .liftAndDrag {
            print("拖动操作开始")
        }
    }

    [...]
    
    let interaction = ImageAnalysisInteraction()
    var imageView: UIImageView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let imageView = imageView else { return }
        
        view.addSubview(imageView)
        imageView.addInteraction(interaction)
        interaction.delegate = self
    }
}

这个方法会在用户完成长按抬起、正式进入拖动阶段时触发,通过判断interactionType为.liftAndDrag,可以精准捕捉拖动开始的时机。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 04:25:08