2014-04-28 2 views

답변

1

(의 mouseOver 이벤트처럼 나쁜 GUI입니다
"설교 (어쨌든 무시할 것입니다 ...-))

#import "MoTableView.h" 

@implementation MoTableView 
{ 
    NSUInteger mouseRow; 
    NSRect mouseRowFrame; 
} 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
     mouseRow = -1; 
    } 
    return self; 
} 

- (void)awakeFromNib 
{ 
    [self.window setAcceptsMouseMovedEvents:YES]; 
} 

- (void)drawRect:(NSRect)dirtyRect 
{ 
    [super drawRect:dirtyRect]; 

    // Drawing code here. 
    [[NSColor redColor] set]; 
    NSLogDebug(@"mouseRowFrame: %@", NSStringFromRect(mouseRowFrame)); 
    NSFrameRectWithWidth(mouseRowFrame, 2.); 
} 

- (void)mouseMoved:(NSEvent *)theEvent 
{ 
    NSPoint mouseLocation = [theEvent locationInWindow]; 
    NSPoint viewLocation = [self convertPoint:mouseLocation fromView:nil] ; 
    NSInteger row = [self rowAtPoint:viewLocation]; 
    if (row != mouseRow) { 
     mouseRowFrame = [self rectOfRow:row]; 
     [self setNeedsDisplay]; 
     mouseRow = row; 
    } 
} 

@end 
0

하위 클래스를 만들고 추적 영역을 사용해야합니다. 이것이 마우스 버튼을 추적하는 데 사용됩니다.

정확하게 당신이 필요하지 애플 샘플 코드있다 - 호버에 행을 강조 :

HoverTableDemo

그것은 철저하게 WWDC 2011 Session 120

4

에서 논의 된 것 그것은 나에게 그것을 작동하는 데 시간이 걸렸다 기반 에힌트.

내가 틀렸다면 그것은 나를 위해 일하고, 나를 바로 잡습니다. 맥 OS 10.12.2와 엑스 코드 8.2.1

// 
// Created by longkai on 30/12/2016. 
// Copyright (c) 2016 xiaolongtongxue.com. All rights reserved. 
// 

import Cocoa 

class InboxTableCellView: NSTableCellView { 
    // MARK: - Outlets 
    @IBOutlet weak var title: NSTextField! 
    @IBOutlet weak var sender: NSTextField! 
    @IBOutlet weak var time: NSTextField! 
    @IBOutlet weak var snippet: NSTextField! 

    // MARK: - Mouse hover 
    deinit { 
     removeTrackingArea(trackingArea) 
    } 

    private var trackingArea: NSTrackingArea! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.trackingArea = NSTrackingArea(
      rect: bounds, 
      options: [NSTrackingAreaOptions.activeAlways, NSTrackingAreaOptions.mouseEnteredAndExited,/* NSTrackingAreaOptions.mouseMoved */], 
      owner: self, 
      userInfo: nil 
     ) 
     addTrackingArea(trackingArea) 
    } 

    override func draw(_ dirtyRect: NSRect) { 
     super.draw(dirtyRect) 

     NSColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 1.00).set() 

     // mouse hover 
     if highlight { 
      let path = NSBezierPath(rect: bounds) 
      path.fill() 
     } 

     // draw divider 
     let rect = NSRect(x: 0, y: bounds.height - 2, width: bounds.width, height: bounds.height) 
     let path = NSBezierPath(rect: rect) 
     path.fill() 
    } 

    private var highlight = false { 
     didSet { 
      setNeedsDisplay(bounds) 
     } 
    } 

    override func mouseEntered(with event: NSEvent) { 
     super.mouseEntered(with: event) 
     if !highlight { 
      highlight = true 
     } 
    } 

    override func mouseExited(with event: NSEvent) { 
     super.mouseExited(with: event) 
     if highlight { 
      highlight = false 
     } 
    } 
} 
+0

는 나에게 치료를했다 – joe

관련 문제