Objective c 在cocoa窗口控制器中获取鼠标事件

Objective c 在cocoa窗口控制器中获取鼠标事件,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我应该如何在Cocoa窗口控制器中获取鼠标事件,还是应该尝试其他方法 我正在设计一个功能,当鼠标悬停在文本字段的区域上时,文本字段将转换为一个大加号。我建议对NSTextField进行子类化,并在那里处理事件。正如特洛伊敌人所说,它内置了鼠标处理功能。另外,您描述的功能听起来像是您可能在同一个应用程序或其他应用程序中再次使用的功能。只需将类设置为自定义NSTextField即可节省时间 它可能看起来像这样: dchovertextfield.h #import <Cocoa/Cocoa.h

我应该如何在Cocoa窗口控制器中获取鼠标事件,还是应该尝试其他方法


我正在设计一个功能,当鼠标悬停在文本字段的区域上时,文本字段将转换为一个大加号。

我建议对NSTextField进行子类化,并在那里处理事件。正如特洛伊敌人所说,它内置了鼠标处理功能。另外,您描述的功能听起来像是您可能在同一个应用程序或其他应用程序中再次使用的功能。只需将类设置为自定义NSTextField即可节省时间

它可能看起来像这样:

dchovertextfield.h

#import <Cocoa/Cocoa.h>

/** An `NSTextField` subclass that supports mouse entered/exited events.
 */
@interface DCOHoverTextField : NSTextField

@end

创建子类后,进入Interface Builder,选择NSTextField并将类更改为您创建的子类。

iPhone或类似iPad的PC中没有鼠标/光标概念,您如何将鼠标悬停在iPad/iPhone上??:PSounds类似于您应该创建一个自定义的
NSView
子类,其中包含文本字段
NSView
有许多与鼠标相关的事件方法。@Xman:问题被标记为
osx
cocoa
,而不是
ios
cocoa touch
。在OSX上,有鼠标/光标的概念。@oasisweng:好的,我的坏。。。
#import "DCOHoverTextField.h"

@interface DCOHoverTextField()

/* Holds the tracking area for the `NSTextField`. */
@property (strong) NSTrackingArea *trackingArea;

@end

@implementation DCOHoverTextField

- (void)updateTrackingAreas {
    // Remove tracking area if we have one
    if(self.trackingArea) {
        [self removeTrackingArea:self.trackingArea];
    }

    // Call super
    [super updateTrackingAreas];

    // Create a new tracking area
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                     options: NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
                                                       owner:self
                                                    userInfo:nil];

    // Add it
    [self addTrackingArea:self.trackingArea];
}

- (void)mouseEntered:(NSEvent *)theEvent {
    // TODO: Change text field into a plus sign.
}

- (void)mouseExited:(NSEvent *)theEvent {
    // TODO: Change text field back into a regular text field.
}

@end