5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nextEventMatchingMaskの使い方

Last updated at Posted at 2013-12-05

#目的
nextEventMatchingMaskの使い方について。

Mac Dev Centerにあるサンプル Sketchのコード読み中。

ドローツールのようなものを作ろうとすると「ボタン押下→移動→ボタン離す」で移動や描画などを行う事が多くあるが、 mouseDown, mouseMoved, mouseUpメソッドの組み合わせで作ると処理がばらばらになってコードの見通しが悪くなることがある。

mouseDownをトリガにして、mouseUpまで一つのループで書きたいという場合に nextEventMatchingMaskを使うと良い。

#使い方
カスタムビューを作って以下のように書く

-(void)mouseDown:(NSEvent*)theEvent
{
    do {
        NSPoint p = [theEvent locationInWindow];

        // p = [self convertPoint:p fromView:nil];
        
        NSLog(@"%.0f, %.0f", p.x, p.y]);
        
        theEvent = [NSApp nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)
                                      untilDate:[NSDate distantFuture]
                                         inMode:NSEventTrackingRunLoopMode
                                        dequeue:YES];
        
        if ([theEvent type] == NSLeftMouseUp)
            break;
        
    } while (YES);
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?