Contents

beginDraggingSession(items:gesture:source:)

Initiates a drag operation using a gesture recognizer.

Declaration

func beginDraggingSession(items: [NSDraggingItem], gesture: NSGestureRecognizer, source: any NSDraggingSource) -> NSDraggingSession?

Parameters

  • items:

    The dragging items. The frame property of each Nsdraggingitem must be in the view’s coordinate system.

  • gesture:

    The gesture recognizer initiating the drag session.

  • source:

    An object that controls the dragging operation and conforms to the Nsdraggingsource protocol.

Return Value

The dragging session for the drag, or nil if the method can’t start the session.

Discussion

Like beginDraggingSession(with:event:source:), this method starts an internal tracking loop. If the gesture is mouse-based, the system cancels the gesture at the end of the drag-and-drop operation. If the gesture is touch-based, the system cancels the gesture at the start of the drag-and-drop operation.

The system may animate drag images from their initial positions into a system-defined formation. The system clips the drag to the visible area of the view.

var dragSession: NSDraggingSession?

@objc func handleDragGesture(_ gestureRecognizer: NSGestureRecognizer) {
    if gestureRecognizer.state == .began {
        let items = [NSDraggingItem(pasteboardWriter: myPasteboardItem)]
        
        // Returns nil if the drag fails to start.
        dragSession = self.beginDraggingSession(items: items, gesture: gestureRecognizer, source: self)
    }
}

See Also

Related Documentation

Dragging Operations