本文介绍了我应该在我的NSCell上调用什么方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义NSCell编写了一个自定义NSControl。它是一个控件,所以它必须响应鼠标。我创建了一个NSTrackingArea在我的控制,实现 -mouseEntered: -mouseExited: mouseMoved:。 (我必须实现 -mouseUp / Down:,但我不知道在那里做什么,所以现在我没有重写这些方法)。在这些方法中,我成功地确定了鼠标当前是在哪个单元格。现在我有两个问题:




  • 这是跟踪鼠标的好方法吗?

  • 当鼠标进入单元格时,当鼠标离开单元格等时,我应该在鼠标单击时调用NSCell的方法是什么?


  • 所以,基本上:我应该什么时候调用NSCell上的什么方法让我们它会响应鼠标事件?



    EDIT:

    重新读取文档, NSCell的 -trackMouse:inRect:ofView:untilMouseUp:并覆写 -startTrackingAt:inView: -continueTracking:at:inView: -stopTracking:at:inView:mouseIsUp:。再两个问题:1)文档给的印象这些只有当鼠标下来时才调用。那是对的吗?那么我该怎么办呢? 2)在哪里/何时应该调用NSCell的 -trackMouse:inRect:ofView:untilMouseUp:

    解决方案

    我最终实现了自己的鼠标跟踪机制:

      // MyControl.m:

    - (void)mouseDown:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
    if(currentCellIndex< [cell count]){
    MKListCell * cell = [cells objectAtIndex:currentCellIndex];
    currentCell = cell;
    [currentCell mouseDown:theEvent];
    }
    }

    - (void)mouseUp:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView :零]];
    if(currentCellIndex< [cell count]){
    MKListCell * cell = [cell objectAtIndex:currentCellIndex];
    [mouseUp:theEvent];
    }
    }

    - (void)mouseEntered:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView :零]];
    if(currentCellIndex< [cell count]){
    MKListCell * cell = [cell objectAtIndex:currentCellIndex];
    currentCell = cell;
    [currentCell mouseEntered:theEvent];
    }
    }

    - (void)mouseExited:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView :零]];
    if(currentCellIndex< [cell count]){
    MKListCell * cell = [cell objectAtIndex:currentCellIndex];
    [cell mouseExited:theEvent];
    currentCell = nil;
    }
    }

    - (void)mouseMoved:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView :零]];
    MKListCell * cell;
    if(currentCellIndex< [cell count]){
    cell = [cell objectAtIndex:currentCellIndex];
    }
    if(currentCell!= cell){
    [currentCell mouseExited:theEvent];
    [cell mouseEntered:theEvent];
    currentCell = cell;
    }
    else {
    [currentCell mouseMoved:theEvent];
    }
    }

    - (void)mouseDragged:(NSEvent *)theEvent {
    int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView :零]];
    MKListCell * cell = nil;
    if(currentCellIndex< [cell count]){
    cell = [cell objectAtIndex:currentCellIndex];
    }
    if(currentCell!= cell){
    [currentCell mouseExited:theEvent];
    [cell mouseEntered:theEvent];
    currentCell = cell;
    }
    else {
    [currentCell mouseMoved:theEvent];
    }
    }

    - (int)indexOfCellAtPoint:(NSPoint)p {
    int cellIndex =(self.bounds.size.height - p.y)/ cellHeight;
    return cellIndex;
    }

    当然,在 MyCell.h

       - (void)mouseDown:(NSEvent *)event; 
    - (void)mouseUp:(NSEvent *)event;
    - (void)mouseMoved:(NSEvent *)event;
    - (void)mouseEntered:(NSEvent *)event;
    - (void)mouseExited:(NSEvent *)event;

    使用这些方法的空实现(因此编译器不会抱怨,的鼠标处理方法子类化)。


    I am writing a custom NSControl with custom NSCells. It is a control, so it has to respond to the mouse. I created an NSTrackingArea over my control, implemented -mouseEntered:, -mouseExited: and -mouseMoved:. (And I will have to implement -mouseUp/Down:, but I have no idea what to do in there, so for now I haven't overridden those methods yet.) In these methods I successfully determine on which cell the mouse currently is. Now I have two questions:

    • Is this a good approach for tracking the mouse? If not, what should I do instead?
    • What method should I call on my NSCell on a mouse click, when the mouse enters the cell, when the mouse leaves the cell etc? Apple's docs are not very clear about this.

    So, basically: When should I call what method on my NSCell to let it respond to mouse events?

    EDIT:
    Rereading the docs, I think I should call NSCell's -trackMouse:inRect:ofView:untilMouseUp: and override -startTrackingAt:inView:, -continueTracking:at:inView: and -stopTracking:at:inView:mouseIsUp:. Again two questions: 1) the docs give the impression these are only called when the mouse is down. Is that correct? Then what should I do instead? 2) Where/when should I call NSCell's -trackMouse:inRect:ofView:untilMouseUp:?

    解决方案

    I ended up implementing my own mouse tracking mechanism:

    // MyControl.m:
    
    - (void)mouseDown:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        if (currentCellIndex < [cells count]) {
            MKListCell *cell = [cells objectAtIndex:currentCellIndex];
            currentCell = cell;
            [currentCell mouseDown:theEvent];
        }
    }
    
    - (void)mouseUp:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        if (currentCellIndex < [cells count]) {
            MKListCell *cell = [cells objectAtIndex:currentCellIndex];
            [cell mouseUp:theEvent];
        }
    }
    
    - (void)mouseEntered:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        if (currentCellIndex < [cells count]) {
            MKListCell *cell = [cells objectAtIndex:currentCellIndex];
            currentCell = cell;
            [currentCell mouseEntered:theEvent];
        }
    }
    
    - (void)mouseExited:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        if (currentCellIndex < [cells count]) {
            MKListCell *cell = [cells objectAtIndex:currentCellIndex];
            [cell mouseExited:theEvent];
            currentCell = nil;
        }
    }
    
    - (void)mouseMoved:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        MKListCell *cell;
        if (currentCellIndex < [cells count]) {
            cell = [cells objectAtIndex:currentCellIndex];
        }
        if (currentCell != cell) {
            [currentCell mouseExited:theEvent];
            [cell mouseEntered:theEvent];
            currentCell = cell;
        }
        else {
            [currentCell mouseMoved:theEvent];
        }
    }
    
    - (void)mouseDragged:(NSEvent *)theEvent {
        int currentCellIndex = [self indexOfCellAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
        MKListCell *cell = nil;
        if (currentCellIndex < [cells count]) {
            cell = [cells objectAtIndex:currentCellIndex];
        }
        if (currentCell != cell) {
            [currentCell mouseExited:theEvent];
            [cell mouseEntered:theEvent];
            currentCell = cell;
        }
        else {
            [currentCell mouseMoved:theEvent];
        }
    }
    
    - (int)indexOfCellAtPoint:(NSPoint)p {
        int cellIndex = (self.bounds.size.height - p.y) / cellHeight;
        return cellIndex;
    }
    

    And of course, in MyCell.h:

    - (void)mouseDown:(NSEvent *)event;
    - (void)mouseUp:(NSEvent *)event;
    - (void)mouseMoved:(NSEvent *)event;
    - (void)mouseEntered:(NSEvent *)event;
    - (void)mouseExited:(NSEvent *)event;
    

    With an empty implementation for those methods (so the compiler doesn't complain and I can leave the implementation of the mouse handling methods to subclasses).

    这篇关于我应该在我的NSCell上调用什么方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:21