2016-05-16 9 views
0

Swiftを使用してOS X用のCocoaアプリケーションを作成しています。アプリケーションは非常にシンプルであると考えられています:基本的に画像を表示し、その画像内のマウスクリックを待ち受けて、画像の原点(絶対的なマウス座標ではありません)に基づいて2Dマウス座標を返します。私はそれがうまく説明されているかどうかはわかりませんが、たとえばマウスクリックイベントを登録すると、マウスクリックが画像の0,0ポイントから右に23ピクセル、下に57ピクセルまたはユニットがどんなものであろうと)。Swift - UIElement内のマウスクリック座標を取得する

これまでのところ、私はこれを持っていますが、私が行うことができましたすべてが、それは絶対にマウスを返すために取得を調整している。

import Cocoa 

class ViewController: NSViewController { 

    @IBOutlet var ImageButton: NSButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let fileName = "myTestImage.jpg" 
     let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! + "/TrainingSet/" + fileName 
     //"/Users/dan/Documents/myTestImage.jpg" 
     let myImage = NSImage(contentsOfFile: path) 
     ImageButton.image = myImage 

    } 

    override var representedObject: AnyObject? { 
     didSet { 
     // Update the view, if already loaded. 
     } 
    } 

    @IBAction func ImageButtonClicked(sender: NSButton) { 

     //let x = sender 
     //let coords = x.frame 

     let mouseLocation = NSEvent.mouseLocation(); 
     print("Mouse Location X,Y = \(mouseLocation)") 
     print("Mouse Location X = \(mouseLocation.x)") 
     print("Mouse Location Y = \(mouseLocation.y)") 
    } 

} 

は、どのように私は、私は必要な情報を得ることについて行くのでしょうか?

+1

について何をsender.superview.convertPoint(: mouseLocation、toView)を試したことがありますか? – xhamr

答えて

1

sender.convertPoint(mouseLocation, fromView:nil) 
関連する問題