Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未触发绘图交互项启动_Ios_Swift - Fatal编程技术网

Ios 未触发绘图交互项启动

Ios 未触发绘图交互项启动,ios,swift,Ios,Swift,我有一个包含两个标签的xib文件,我希望这些标签可以拖动。问题是itemsForBeginning函数从未启动!我不确定我的代码中缺少了什么 class EditSchduleLeftSide: UIView { @IBOutlet weak var collaboration: UILabel! @IBOutlet weak var deepwork: UILabel! override func awakeFromNib() { super.awak

我有一个包含两个标签的xib文件,我希望这些标签可以拖动。问题是itemsForBeginning函数从未启动!我不确定我的代码中缺少了什么

class EditSchduleLeftSide: UIView {
    @IBOutlet weak var collaboration: UILabel!
    @IBOutlet weak var deepwork: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.isUserInteractionEnabled = true

        addDragInteraction()
    }

    func addDragInteraction(){
        let interaction = UIDragInteraction(delegate: self)
        self.addInteraction(interaction)
    }
}

extension EditSchduleLeftSide: UIDragInteractionDelegate {

    func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
        let hitPoint = session.location(in: self)
        if let hittedLabel = hitTest(hitPoint, with: nil)  as? UILabel {
            let provider = NSItemProvider(object: hittedLabel.text as! NSString)
            let dragItem = UIDragItem(itemProvider: provider)
            return [dragItem]
        }
        return []
    }
}

我尝试了你的代码,并更改了你的
addDragInteraction
方法

func addDragInteraction(){
    let interaction = UIDragInteraction(delegate: self)
    interaction.isEnabled = true
    self.addInteraction(interaction)
}
另外,在拖动之前,您需要按住任何标签,就像长按一样

希望这是有帮助的

完整代码:

class TmpView: UIView {

class func instanceFromNib() -> TmpView {
    return UINib(nibName: "TmpView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! TmpView
}

@IBOutlet weak var collaboration: UILabel!
@IBOutlet weak var deepwork: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    self.isUserInteractionEnabled = true
    collaboration.isUserInteractionEnabled = true
    deepwork.isUserInteractionEnabled = true
    self.layer.borderWidth = 2
    addDragInteraction()
}

func addDragInteraction(){
    let interaction = UIDragInteraction(delegate: self)
    interaction.isEnabled = true
    self.addInteraction(interaction)
}
}

extension TmpView: UIDragInteractionDelegate {

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
    let hitPoint = session.location(in: self)
    if let hittedLabel = hitTest(hitPoint, with: nil)  as? UILabel {
        let provider = NSItemProvider(object: hittedLabel.text! as NSString)
        let dragItem = UIDragItem(itemProvider: provider)
        return [dragItem]
    }
    return []
}
}
您代码的最终结果:


你能和我分享一下源代码吗?因为它在我这边仍然不起作用,所以只需在代码中替换上面的函数,然后按住任何一个标签,然后尝试拖动。并确保标签的
isUserInteractionEnabled
。不要尝试拖动
UIView
,尝试拖动
UILabel
,只需按住UILable一秒钟。同样,不工作。。如果你能分享整个项目,我将不胜感激。。那会有帮助的好吧,我想这就是问题所在。。我也为标签设置了isUserInteractionEnabled=true。谢谢