Swift: 드래그하여 NSColor 복사
9498 단어 Swift
demo
출처 import Cocoa
extension NSColor {
var swatch: NSImage {
let size = NSSize(width: 25.0, height: 25.0)
let image = NSImage(size: size)
image.lockFocus()
self.drawSwatch(in: NSRect(origin: .zero, size: size))
image.unlockFocus()
return image
}
}
class ColorView: NSView {
var color: NSColor = .white
required init?(coder: NSCoder) {
super.init(coder: coder)
registerForDraggedTypes([.color])
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
color.setFill()
NSBezierPath(rect: dirtyRect).fill()
}
override func mouseDragged(with event: NSEvent) {
let draggingItem = NSDraggingItem(pasteboardWriter: color)
let swatch = color.swatch
draggingItem.setDraggingFrame(CGRect(origin: .zero, size: swatch.size), contents: swatch)
beginDraggingSession(with: [draggingItem], event: event, source: self)
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if let _ = NSColor(from: sender.draggingPasteboard) {
return .copy
}
return []
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
if let _ = NSColor(from: sender.draggingPasteboard) {
return .copy
}
return []
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let newColor = NSColor(from: sender.draggingPasteboard) else {
return false
}
color = newColor
self.needsDisplay = true
return true
}
}
extension ColorView: NSDraggingSource {
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return .copy
}
}
Reference
이 문제에 관하여(Swift: 드래그하여 NSColor 복사), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Kyome/items/0c30b604029aa4c390e1
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import Cocoa
extension NSColor {
var swatch: NSImage {
let size = NSSize(width: 25.0, height: 25.0)
let image = NSImage(size: size)
image.lockFocus()
self.drawSwatch(in: NSRect(origin: .zero, size: size))
image.unlockFocus()
return image
}
}
class ColorView: NSView {
var color: NSColor = .white
required init?(coder: NSCoder) {
super.init(coder: coder)
registerForDraggedTypes([.color])
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
color.setFill()
NSBezierPath(rect: dirtyRect).fill()
}
override func mouseDragged(with event: NSEvent) {
let draggingItem = NSDraggingItem(pasteboardWriter: color)
let swatch = color.swatch
draggingItem.setDraggingFrame(CGRect(origin: .zero, size: swatch.size), contents: swatch)
beginDraggingSession(with: [draggingItem], event: event, source: self)
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if let _ = NSColor(from: sender.draggingPasteboard) {
return .copy
}
return []
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
if let _ = NSColor(from: sender.draggingPasteboard) {
return .copy
}
return []
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let newColor = NSColor(from: sender.draggingPasteboard) else {
return false
}
color = newColor
self.needsDisplay = true
return true
}
}
extension ColorView: NSDraggingSource {
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return .copy
}
}
Reference
이 문제에 관하여(Swift: 드래그하여 NSColor 복사), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Kyome/items/0c30b604029aa4c390e1텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)