调用了CursorUpdate,但未更新光标 [英] cursorUpdate called, but cursor not updated

查看:33
本文介绍了调用了CursorUpdate,但未更新光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了几个小时,不知道出了什么问题。我想为作为NSTextView子视图的按钮定制光标,我添加了一个跟踪区域,并在鼠标输入按钮时发送CursorUpdate消息。

每次鼠标进入跟踪区域时都会调用cursorUpdate方法。但是光标保持在IBeamCursor上。

有什么想法吗?

苹果文档参考:managing cursor-update event

- (void)cursorUpdate:(NSEvent *)event {
    [[NSCursor arrowCursor] set];
}

- (void)myAddTrackingArea {
    [self myRemoveTrackingArea];

    NSTrackingAreaOptions trackingOptions = NSTrackingCursorUpdate | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
    _trackingArea = [[NSTrackingArea alloc] initWithRect: [self bounds] options: trackingOptions owner: self userInfo: nil];
    [self addTrackingArea: _trackingArea];
}

- (void)myRemoveTrackingArea {
    if (_trackingArea)
    {
        [self removeTrackingArea: _trackingArea];
        _trackingArea = nil;
    }
}

推荐答案

我遇到了同样的问题。

问题是,NSTextView每次收到mouseMoved:事件时都会更新其游标。该事件由NSTextView的自我更新NSTrackingArea触发,该事件始终跟踪NSTextViewNSScrollView中的可见部分。所以我可能有两个解决方案。

  1. 覆盖updateTrackingAreas删除Cocoa提供的跟踪区域,并确保您始终创建一个新区域,而不是该按钮。(我不会这么做的!)

  2. 覆盖mouseMoved:,并确保当光标位于按钮上时不会调用SUPERR。

    - (void)mouseMoved:(NSEvent *)theEvent {
        NSPoint windowPt = [theEvent locationInWindow];
        NSPoint superViewPt = [[self superview]
              convertPoint: windowPt  fromView: nil];
        if ([self hitTest: superViewPt] == self) {
            [super mouseMoved:theEvent];
        }
    }
    

这篇关于调用了CursorUpdate,但未更新光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆