2017-12-10 18 views
1

マウスで表示するための簡単な解決策が見つかりません。すべてtutorialsNSBezierPathを使用して線や矩形などを描きますが、私はこのようなマウスで簡単なアプリケーションの描画が必要です:http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/010-NSView.plココアスウィフトでマウスを使った簡単な描画

私はそれを変換しようとしました。しかし、何も私のために働かない:

var lastPoint = NSZeroPoint 
    var red: CGFloat = 0.0 
    var green: CGFloat = 0.0 
    var blue: CGFloat = 0.0 
    var brushWidth: CGFloat = 10.0 
    var opacity: CGFloat = 1.0 
    var swiped = false 

override func mouseUp(with event: NSEvent) { 
     if !swiped { 
      drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint) 
     } 

    } 

    override func mouseDragged(with event: NSEvent) { 
     swiped = true 
     let currentPoint = event.locationInWindow 
     drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint) 
     lastPoint = currentPoint 
    } 


    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) { 
     print("drag") 
     var ctx = NSGraphicsContext.current 
     ctx?.cgContext.setStrokeColor(CGColor(red: 255, green: 0, blue: 0, alpha: 1.0)) 
     ctx?.cgContext.setLineWidth(5.0) 
     ctx?.cgContext.beginPath() 
     ctx?.cgContext.move(to: fromPoint) 
     ctx?.cgContext.addLine(to: toPoint) 
     ctx?.cgContext.drawPath(using: .fillStroke) 
     ctx?.cgContext.closePath() 
    } 

私はマウスイベントで何かを描くことができ、簡単なデモを作成し、単一点

答えて

3

を描くことができる方法を提案してください。あなたがなど

import Cocoa 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate { 

@IBOutlet weak var window: NSWindow! 

func applicationDidFinishLaunching(_ aNotification: Notification) { 
    // Insert code here to initialize your application 
    let view = DrawView() 
    view.translatesAutoresizingMaskIntoConstraints = false 
    self.window.contentView?.addSubview(view) 

    self.window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options: [], metrics: nil, views: ["view": view])) 
    self.window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: [], metrics: nil, views: ["view": view])) 
} 

func applicationWillTerminate(_ aNotification: Notification) { 
    // Insert code here to tear down your application 
} 

} 

class DrawView: NSView { 
var path: NSBezierPath = NSBezierPath() 

override func mouseDown(with event: NSEvent) { 
    path.move(to: convert(event.locationInWindow, from: nil)) 
    needsDisplay = true 
} 

override func mouseDragged(with event: NSEvent) { 
    path.line(to: convert(event.locationInWindow, from: nil)) 
    needsDisplay = true 
} 

override func draw(_ dirtyRect: NSRect) { 
    super.draw(dirtyRect) 

    NSColor.black.set() 

    path.lineJoinStyle = .roundLineJoinStyle 
    path.lineCapStyle = .roundLineCapStyle 
    path.lineWidth = 10.0 
    path.stroke() 
} 
} 

enter image description here

+0

素晴らしいおかげで色や線幅のようにしたい場合は、任意のパラメータを変更することができますが、なぜOBJに、答えることができる - すべての例は、NSGraphicsContextおよびIOS UIGraphicsContext上を使用するCが、ココアでは、NSBezierPathを使用します。 – Arti

+1

@Artiあなたの要件には、そうする方法はたくさんあります。私は仕事をする最も簡単な方法を選んでいます:]異なる技術の違いは何ですか:https://www.evanmiller .org/notes-on-2d-graphics-mac.html – brianLikeApple

+0

「NSBezierPath」は、「ポイント・ツー・ポイント描画」とは異なる抽象化です。 'NSGraphicsContext'を使って' CGMutablePath'を描画することもできますが、APIは非常に古く、Cコードを書くような感じがします。 – ctietze

関連する問題