1
0

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.

mouseMovedをキャッチする方法

Posted at

デフォルトではmouseMovedをオーバライドしてもメッセージを受け取ってくれない。
(それなりの理由はあるのだろうが、ややこしい事を・・・と思わなくもない)

やり方としてはObjective-CではNSWindowで以下のようなやり方があるらしい。(試してない)

-(void)awakeFromNib
{
[self setAcceptsMouseMovedEvents:YES];
[self makeFirstResponder:self];
}

SwiftになってからはNSViewやNSImageViewでNSTrackingAreaの初期化時に指定するオプションでメッセージを受け取るようにするのがいいみたいです。


class customeImageView: NSImageView {
  
    override func awakeFromNib() {
        super.awakeFromNib()
        configure()
    }
    
    
    func configure() {
        let options:NSTrackingAreaOptions = [
            .mouseMoved,
            .cursorUpdate,
            .activeAlways,
            .enabledDuringMouseDrag
        ]
        let trackingArea = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil)
        addTrackingArea(trackingArea)
    }

    override func mouseDown(with event: NSEvent) {
        NSLog("mouseDown")
        
    }
    
    override func mouseDragged(with event: NSEvent) {
        NSLog("mouseDragged")
    }
    
    override func mouseMoved(with event: NSEvent) {
        NSLog("mouseMoved")
    }
   
}

参考サイト

NSTrackingAreaの初期化

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?