Cocoa-EventHandle

欧阳昊焱
2023-12-01

basic mouse event

mouse down, mouse dragging, mouse up, and mouse movement.

responder chain

view‘s next responder is its super view
generally a view insert into a view hierarchy ,this view is automatically as its subView’s next responder
never call setNextResponder method

firstResponder

如果一个view作为firstResponder 则这个view会先于其他对象接受到他们发送的消息的回应
通常表现比如文本框如果作为firstResponder就会有竖杠闪烁

general view only receive mouse-down message which is not as firstResponde,但是这个view必须显示在window上 并且是可见的
比如某个文本框并不是firstResponder,但是如果他被鼠标点击了 它就会作为firstResponder,所以它接受到了mouseDown

成为firstReponder

view.window makeFirstResponder:一个NSView或者NSTextField
//方法二
[NSTestField becomeFirstResponder];//不建议这样调用

element调用这个方法并不是一定生效,调用后首先会询问当前的first responder 让他resign,如果他同意了那会再查看一下目标element的canBecomeFirstResponder 如果可以然后会再查看这个element是否在active view hierarchy(如果element所属的window是一个valid window 则表示是在active view hierarchy) 这样才会将element置为first responder
这个responder cain的root 一般是application的key Window

一般是子类重写becomeFirstResponder方法里面添加对text filer的修饰 实现highlighting

acceptsFirstResponder

default NSResponder instance return NO
you can override this method

- (BOOL)acceptsFirstResponder
{
    return YES;
}

becomeFirstResponder

if a view receive this message ,is mean that window attemps to make the view first responder,default implementation this message return YES

resignFirstResponder

if you want to resign a view as firstResponder call this message

key view loop

key view loop let user can user press “tab” keys switch between views in a window
so if a view can become firstResponder ,this view must in key view loop
you can set a view as other view’s next key view by setting view’snextKeyView

target-action paradigm

控件向目标对象发送相应用户操作的操作信息 比如说一个按钮 单击该按钮会将操作消息发送到按钮的目标对象 如果未指定特定的目标对象 则会将其发送到响应者链的上游

获取鼠标点击的位置

通过locationInWindow获取鼠标点击在window的坐标,再通过convertPoint转换到view坐标系中

-(void)mouseDown:(NSEvent *)event
{
    NSPoint clickLocation;
	clickLocation = [self convertPoint:[event locationInWindow]
                  fromView:nil];
}

关于自己实现mouseMove这些message看这里

苹果开发文档

 类似资料: