將您嵌入NSSlider
在NSView
。
子類的NSView和捕捉鼠標移動事件
@interface PBView : NSView {
id delegate;
}
@property (assign)id delegate;
@end
@implementation PBView
@synthesize delegate;
-(void) mouseMoved: (NSEvent *) thisEvent
{
NSPoint cursorPoint = [ thisEvent locationInWindow ];
[delegate sliderValueChanged];
}
- (void)createTrackingArea
{
NSTrackingAreaOptions focusTrackingAreaOptions = NSTrackingMouseMoved;
focusTrackingAreaOptions |= NSTrackingActiveInActiveApp;
focusTrackingAreaOptions |= NSTrackingMouseEnteredAndExited;
focusTrackingAreaOptions |= NSTrackingAssumeInside;
focusTrackingAreaOptions |= NSTrackingInVisibleRect;
NSTrackingArea *focusTrackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
options:focusTrackingAreaOptions owner:self userInfo:nil];
[self addTrackingArea:focusTrackingArea];
}
- (void)awakeFromNib
{
[self createTrackingArea];
}
@end
現在實行
-(void)sliderValueChanged
{
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation]; // mouse location
NSRect r= [window frame];// window location
NSLog(@"%@", NSStringFromPoint(r.origin));
NSLog(@"%@", NSStringFromPoint(mouseLoc));
[silder setIntValue:(mouseLoc.x -r.origin.x)];//silder is object of NSSlider
}
這裏是共享項目的HTTPS鏈接://www.idrive。 com/idrive/sh/sh?k = n9a5b8a8w7 –
夢幻般的考試謝謝,非常感謝。 我做了完全一樣的事情,但是,我不能移動滑塊而不點擊。 讓我明白,您的意思 「將您的NSSlider嵌入到NSView中」爲: 1.從庫右下角選擇任意滑塊並將其放在窗口上 2.選擇編輯器 - >嵌入 - >自定義視圖 正確嗎? 除嵌入和編碼之外,還有什麼特別的事情需要我去做嗎? 那麼我認爲這個問題是我的PBView(嵌入滑塊)沒有連接任何東西(從連接檢查器判斷),雖然你的是。你能告訴我在哪裏/如何連接它? – user2549858
將自定義視圖類更改爲PBView(NSView - > PBView) –