CocoaのためのCポインタの取り扱いその1 [Swift]
Appkitなどで、引数としてCのポインタを扱うような場合がある。Objective-CならそのままCを混ぜればいいんだけれど、Swiftの場合これはどうしたもんだろうか?
typealias NSPointArray = UnsafeMutablePointer<NSPoint>
ん、なにこれ?
Appleのドキュメントがあった!Interacting with C Pointers - Swift Blog - Apple Developer
NSPointerArray
NSPointの配列。
ベジエ曲線を引くときに使う
ああ、Accelerateでもつかうのか、なるほど。
NSPointArrayを渡してやらないといけないのだが...
aBezier.appendBezierPathWithPoints( &points, count: points.count)
これでOK
var points:[NSPoint] = [NSMakePoint(220.0, 50.0), NSMakePoint(220.0,120.0), NSMakePoint(150.0,180.0), NSMakePoint( 80.0,120.0), NSMakePoint( 80.0, 50.0)] aBezier.appendBezierPathWithPoints( &points, count: points.count)
家を書いてみる
//NSBezierPath var aButton011 : NSButton? var aView011 : NSView? //実験用ビューのボタンを押した時に実行されるところ func viewAction011(sender : AnyObject?){ //準備 let aButton = aButton011 let aView = aView011 var theWindow : NSWindow = aButton!.window! //準備ここまで //ビューにフォーカスを当てる aView?.lockFocus() //バックグラウンドカラーを描画 let backgroundColor = NSColor.whiteColor() backgroundColor.setFill() NSRectFill(NSMakeRect(0, 20, 300.0, 178.0)) NSColor.redColor().setStroke() NSColor.magentaColor().setFill() //空のベジェパスを作成 let aBezier : NSBezierPath = NSBezierPath() aBezier.moveToPoint(CGPoint(x: 80.0,y: 50.0)) //ここから var points:[NSPoint] = [NSMakePoint(220.0, 50.0), NSMakePoint(220.0,120.0), NSMakePoint(150.0,180.0), NSMakePoint( 80.0,120.0), NSMakePoint( 80.0, 50.0)] aBezier.appendBezierPathWithPoints( &points, count: points.count) //ここまで aBezier.closePath() aBezier.fill() aBezier.lineWidth = 2.0 aBezier.stroke() //ビューからフォーカスを外す aView?.unlockFocus() NSLog("!!! %@",aBezier.description) } @IBAction func function011(sender: AnyObject) { var aWindow : NSWindow = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300.0, 200.0), styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing: .Buffered, defer: false, screen: NSScreen.mainScreen()) windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄 aWindow.center()//ウインドウをスクリーンの中心に aWindow.title = "NSBezierPath"//タイトル設定 //ボタン var theButton : NSButton = NSButton(frame: NSMakeRect(100.0, 0.0, 100.0, 30.0)) theButton.title = "Action" theButton.bezelStyle = NSBezelStyle.RoundedBezelStyle theButton.action = Selector("viewAction011:") theButton.target = self aWindow.contentView.addSubview(theButton) //ビュー var theView : NSView = NSView(frame: NSMakeRect(0.0, 20.0, 300.0, 100.0)) //レイヤーバックドにするのだ theView.wantsLayer = true aWindow.contentView.addSubview(theView) //実験ウインドウにUIパーツを渡す self.aButton011 = theButton //ボタン self.aView011 = theView //テストビュー aWindow.orderFront(self)//前面に aWindow.makeKeyAndOrderFront(self)//表示 }