Ped*_*ira 3 cocoa objective-c nsbutton nscursor nstrackingarea
好的,问题是:
我有一个NSTextView,我添加自定义NSButton使用:
[_textView addSubview:button];
Run Code Online (Sandbox Code Playgroud)
然后,在我的NSButton子类中,我(以及NSTrackingArea东西):
- (void)mouseEntered:(NSEvent *)event{
[[NSCursor arrowCursor] set];
}
- (void)mouseExited:(NSEvent *)theEvent{
[[NSCursor arrowCursor] set];
}
- (void)mouseDown:(NSEvent *)theEvent{
[[NSCursor arrowCursor] set];
}
- (void)mouseUp:(NSEvent *)theEvent{
[[NSCursor arrowCursor] set];
}
Run Code Online (Sandbox Code Playgroud)
但是当我悬停它时,光标保持不变IBeamCursor(因为它是a NSTextView).只有当我按下按钮时,光标才会更新.然后,当我移动鼠标时,仍然在按钮内,光标返回到IBeamCursor.
关于如何做到这一点的任何想法?谢谢!
添加仅跟踪进入/退出事件的跟踪区域对于NSTextView子视图来说似乎是不够的.不知何故,textview总是赢得并设置它 IBeamCursor.
在子类中添加跟踪区域时,您可以尝试始终启用鼠标移动事件跟踪(NSTrackingMouseMoved)NSButton:
#import "SSWHoverButton.h"
@interface SSWHoverButton()
{
NSTrackingArea* trackingArea;
}
@end
@implementation SSWHoverButton
- (void)mouseMoved:(NSEvent*)theEvent
{
[[NSCursor arrowCursor] set];
}
- (void)updateTrackingAreas
{
if(trackingArea != nil)
{
[self removeTrackingArea:trackingArea];
}
NSTrackingAreaOptions opts = (NSTrackingMouseMoved|NSTrackingActiveAlways);
trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void)dealloc
{
[self removeTrackingArea:trackingArea];
}
@end
Run Code Online (Sandbox Code Playgroud)
Swift 5 变体:
import Cocoa
class InsideTextButton: NSButton {
var trackingArea: NSTrackingArea?
override func mouseMoved(with event: NSEvent) {
NSCursor.arrow.set()
}
override func updateTrackingAreas() {
if let area = trackingArea {
removeTrackingArea(area)
}
trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseMoved, .activeAlways], owner: self, userInfo: nil)
if let area = trackingArea {
addTrackingArea(area)
}
}
deinit {
if let area = trackingArea {
removeTrackingArea(area)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1258 次 |
| 最近记录: |