如何解决拖放在NSImageView Swift MacOS应用程序中渲染的动画GIF
我正在尝试构建一个Mac OSX应用程序,该应用程序呈现多个gif,并允许用户拖放它们以将gif复制到其他应用程序中。我正在使用符合DragDropImageView
的{{1}}(下面的代码)来呈现启用拖放功能的gif。
它工作正常,除了当我将gif拖放到另一个应用程序中时,它仅复制gif的单个图像帧。我的目的是复制整个gif文件,而不仅仅是复制一张图片。
总体上来说,我对iOS / MacOS开发还很陌生,我不确定构建此可拖动gif组件的方法是否正确。
我正在使用swiftUI构建应用程序,并且使用了名为NSImageView
的自定义视图,该视图将GifImageView
转换为swiftUI视图。
DragDropImageView
DragDropImageView.swift
import Cocoa
class DragDropImageView: NSImageView,NSDraggingSource {
/// Holds the last mouse down event,to track the drag distance.
var mouseDownEvent: NSEvent?
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
isEditable = false
}
required init?(coder: NSCoder) {
super.init(coder: coder)
// Assure editable is set to true,to enable drop capabilities.
isEditable = true
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
// MARK: - NSDraggingSource
// Since we only want to copy the current image we register
// for .copy operation.
func draggingSession(_: NSDraggingSession,sourceOperationMaskFor _: NSDraggingContext) -> NSDragOperation {
return NSDragOperation.copy
}
// // Clear the ImageView on delete operation; e.g. the image gets
// // dropped on the trash can in the dock.
// func draggingSession(_: NSDraggingSession,endedAt _: NSPoint,// operation: NSDragOperation) {
// if operation == .delete {
// image = nil
// }
// }
// Track mouse down events and safe the to the poperty.
override func mouseDown(with theEvent: NSEvent) {
mouseDownEvent = theEvent
}
// Track mouse dragged events to handle dragging sessions.
override func mouseDragged(with event: NSEvent) {
// Calculate the dragging distance...
let mouseDown = mouseDownEvent!.locationInWindow
let dragPoint = event.locationInWindow
let dragdistance = hypot(mouseDown.x - dragPoint.x,mouseDown.y - dragPoint.y)
// Cancel the dragging session in case of an accidental drag.
if dragdistance < 3 {
return
}
guard let image = self.image else {
return
}
let draggingImage = image
// Create a new NSDraggingItem with the image as content.
let draggingItem = NSDraggingItem(pasteboardWriter: image)
// Calculate the mouseDown location from the window's coordinate system to the
// ImageView's coordinate system,to use it as origin for the dragging frame.
let draggingFrameOrigin = convert(mouseDown,from: nil)
// Build the dragging frame and offset it by half the image size on each axis
// to center the mouse cursor within the dragging frame.
let draggingFrame = NSRect(origin: draggingFrameOrigin,size: draggingImage.size)
.offsetBy(dx: -draggingImage.size.width / 2,dy: -draggingImage.size.height / 2)
// Assign the dragging frame to the draggingFrame property of our dragging item.
draggingItem.draggingFrame = draggingFrame
// Provide the components of the dragging image.
draggingItem.imageComponentsProvider = {
let component = NSDraggingImageComponent(key: NSDraggingItem.ImageComponentKey.icon)
component.contents = image
component.frame = NSRect(origin: NSPoint(),size: draggingFrame.size)
return [component]
}
// Begin actual dragging session. Woohow!
beginDraggingSession(with: [draggingItem],event: mouseDownEvent!,source: self)
}
}
GifImageView.swift
在我的import AppKit;
import SwiftUI;
struct GifImageView: NSViewRepresentable {
var image: NSImage
func makeNSView(context: Context) -> DragDropImageView {
let view = DragDropImageView()
view.image = self.image
// view.allowsCutcopyPaste = true
return view
}
func updateNSView(_ view: DragDropImageView,context: Context) {
}
}
struct GifImageView_Previews: PreviewProvider {
static var previews: some View {
GifImageView(image: NSImage(data: (NSDataAsset(name: "tenor")?.data)!)!)
}
}
中,我使用ContentView.swift
这样的东西:
GifImageView
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。