macOS/iOS API解説

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

目次

setClip

INDEX>AppKit>NSBezier

現在のクリッピングパスを置き換えます

Objective-C

- (void)setClip

Swift

func setClip()

解説

現在のクリッピングパスを置き換えます。

f:id:jjj777:20150314123700g:plain

返り値

なし

引数

なし

フレームワーク

ApplicationKit

クラス

NSBezierPath

使用可能

10.0

参照


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


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

- saveGraphicsState (NSGraphicsContext)
- restoreGraphicsState (NSGraphicsContext)

関連記事(外部サイト)

例文

Objective-C

#import "MyView.h"

@implementation MyView

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

NSBezierPath *thePath1 = [NSBezierPath bezierPath];
NSBezierPath *thePath2 = [NSBezierPath bezierPath];
NSBezierPath *thePath3 = [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];
//clipセットし直し
[thePath3 appendBezierPathWithOvalInRect:NSMakeRect(0,0,200,200)];
[thePath3 setClip];


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


}

@end

Swift

//
//  TestView035.swift
//  NSBezier
//
//  Created by air on 2015/03/14.
//  Copyright (c) 2015年 oomori. All rights reserved.
//

import Cocoa

class TestView035: NSView {
    //最初のマウスカーソルの位置
    var mouselocation : NSPoint = NSMakePoint(200.0, 400.0)
    
    //描画メソッド
    
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        
        //バックグラウンドカラーを描画
        let backgroundColor = NSColor.whiteColor()
        backgroundColor.setFill()
        NSRectFill(NSMakeRect(0, 20, 300.0, 178.0))
        
        
        
        let myFont : NSFont = NSFont(name: "HiraMaruPro-W4", size: 69.0)!
        //空のベジェパスを作成
        let aBezier : NSBezierPath = NSBezierPath()
        let bBezier : NSBezierPath = NSBezierPath()
        var aGlyphs : [NSGlyph] = [ myFont.glyphWithName("gid935"),//カ
                                    myFont.glyphWithName("gid997"),//ラ
                                    myFont.glyphWithName("gid934"),//オ
                                    myFont.glyphWithName("gid941")//ケ
                                    ]
        
        aBezier.moveToPoint(CGPoint(x: 10.0,y: 75.0))
        aBezier.appendBezierPathWithGlyphs( &aGlyphs , count: 4, inFont: myFont)
        bBezier.moveToPoint(CGPoint(x: 10.0,y: 75.0))
        bBezier.appendBezierPathWithGlyphs( &aGlyphs , count: 4, inFont: myFont)
        
        
        let cBezier : NSBezierPath = NSBezierPath()
        
        cBezier.appendBezierPathWithRect(
                CGRect( x: 10.0,
                        y: 0.0,
                        width:mouselocation.x-10.0,
                        height: 150 ))
            cBezier.addClip()
                    NSColor.redColor().setStroke()
            NSColor.redColor().setFill()
            aBezier.lineWidth = 10.0
            aBezier.fill()
            aBezier.stroke()
        
        
        NSColor.greenColor().setStroke()
        NSColor.greenColor().setFill()
        
        let dBezier : NSBezierPath = NSBezierPath()
        
        dBezier.appendBezierPathWithRect(
            CGRect( x: 0,
                y: 0.0,
                width: 500 ,
                height: 150 ))
        dBezier.setClip()
        bBezier.fill()
    }
    
    //マウスダウンイベント。面倒なのでマウスダウンの後ドラッグやマウスアップの処理もここで行う
    override func mouseDown(theEvent: NSEvent) {
        var newEvent: NSEvent!
        
        //デフォルトマイターリミットを設定
        NSBezierPath.setDefaultMiterLimit(5.0)
        
        //イベント取得
        let maskUp = NSEventMask.LeftMouseUpMask.rawValue
        let maskDragged = NSEventMask.LeftMouseDraggedMask.rawValue
        let maskDown = NSEventMask.LeftMouseDownMask.rawValue
        let mask = Int( maskUp | maskDragged | maskDown ) // cast from UInt
        let anApplication = NSApplication.sharedApplication()
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        }
        
        var keepOn = true
        while keepOn {
            //マウスダウンして以降、左マウスアップするまでのイベントを取得
            newEvent = anApplication.nextEventMatchingMask(
                Int(mask) ,
                untilDate:(NSDate.distantPast() as NSDate),
                inMode:NSDefaultRunLoopMode,
                dequeue:true)
            
            //マウスドラッグイベントがあるか
            if (newEvent != nil) {
                //ある
                
                if newEvent.type == .LeftMouseUp {
                    //Left mouse UPなら
                    keepOn = false
                }else if ((newEvent.type == .LeftMouseDragged)||(newEvent.type == .LeftMouseDown)){
                    //マウスドラッグ
                    mouselocation.x = newEvent.locationInWindow.x-self.frame.origin.x
                    mouselocation.y = newEvent.locationInWindow.y-self.frame.origin.y
                }
                //再描画 できるだけ最小限に絞る。移動した範囲のみなどにする
                //self.setNeedsDisplayInRect(NSMakeRect(mouselocation.x-20.0, mouselocation.y-20.0, 40.0, 40.0))
                //全体の場合はこちら
                self.setNeedsDisplayInRect(self.frame)
                
            }
            
        }
        //マウスイベント終了
        dispatch_async(dispatch_get_main_queue()) {
            NSLog("finishLaunching")
        }
    }
    
}

更新時バージョン

10.10