macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

addClip

INDEX>AppKit>NSBezier

クリッピングパスを追加します

Objective-C

- (void)addClip

Swift

func addClip()

解説

クリッピングパスを追加します。
クリッピングパスに設定した範囲の中が描画されます

f:id:jjj777:20150314114301p:plain

返り値

なし

引数

なし

フレームワーク

ApplicationKit

クラス

NSBezierPath

使用可能

10.0

参照


setClip - Cocoa API解説(iOS/OS X)


clipRect: - Cocoa API解説(iOS/OS X)

関連記事(外部サイト)

例文

Objective-C

#import "MyView.h"

@implementation MyView

//NSViewのサブクラス MyViewのDrawRectに上書き
-(void)drawRect:(NSRect)rect
{

NSBezierPath *thePath1 = [NSBezierPath bezierPath];
NSBezierPath *thePath2 = [NSBezierPath bezierPath];
[thePath1 setWindingRule:NSEvenOddWindingRule];

[thePath1 moveToPoint:NSMakePoint(20,20)];

[thePath1 appendBezierPathWithOvalInRect:NSMakeRect(50,50,100,100)];

//clip
[NSBezierPath clipRect:NSMakeRect(30,30,80,80)];
[thePath2 appendBezierPathWithOvalInRect:NSMakeRect(30,30,70,70)];
[thePath2 addClip];


[[NSColor redColor] set];
[thePath1 fill];


}

@end

Swift

// MARK: NSBezierPath addClip
    var aButton034 : NSButton?
    var aView034 : NSView?
    //実験用ビューのボタンを押した時に実行されるところ
    func viewAction034(sender : AnyObject?){
        //準備
        let aButton = aButton034
        let aView   = aView034
        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 myFont : NSFont = NSFont(name: "HiraMaruPro-W4", size: 69.0)!
        //空のベジェパスを作成
        let aBezier : NSBezierPath = NSBezierPath()
        var aGlyphs : [NSGlyph] = [myFont.glyphWithName("gid1800"),myFont.glyphWithName("gid1801")]
        
        aBezier.moveToPoint(CGPoint(x: 100.0,y: 75.0))
        aBezier.appendBezierPathWithGlyphs( &aGlyphs , count: 2, inFont: myFont)
        
        //NSBezierPath.clipRect(CGRect(x: 50,y: 30,width: 80,height: 80))
        let cBezier : NSBezierPath = NSBezierPath()
       cBezier.appendBezierPathWithOvalInRect(CGRect(x: 80,y: 80,width: 120,height: 120))
        cBezier.addClip()
        
        aBezier.fill()
        
        //ビューからフォーカスを外す
        aView?.unlockFocus()
        NSLog("!!! %@",aBezier.description)
    }
    @IBAction func function034(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("viewAction034:")
        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.aButton034 = theButton    //ボタン
        self.aView034 = theView        //テストビュー
        aWindow.orderFront(self)//前面に
        aWindow.makeKeyAndOrderFront(self)//表示
    }

更新時バージョン

10.10