I have a custom View NSView
and I want to disable userinteraction, but I'm not sure how to do this.
My idea was:
[myView setEnabled:NO];
but it's wrong and doesn't work. How can I make it so that, it's just visible for the user, and nothing else?
NSView doesn't have either setEnabled: or setIgnoresMouseEvents:
Implement the hitTest:
method to return nil
.
From here:
//
// NSView-DisableSubsAdditions.m
// Can Combine Icons
//
// Created by David Remahl on Tue Dec 25 2001.
// Copyright (c) 2001 Infinity-to-the-Power-of-Infinity. All rights reserved.
//
#import "NSView-DisableSubsAdditions.h"
@implementation NSView(DisableSubsAdditions)
- (void)disableSubViews
{
[self setSubViewsEnabled:NO];
}
- (void)enableSubViews
{
[self setSubViewsEnabled:YES];
}
- (void)setSubViewsEnabled:(BOOL)enabled
{
NSView* currentView = NULL;
NSEnumerator* viewEnumerator = [[self subviews] objectEnumerator];
while( currentView = [viewEnumerator nextObject] )
{
if( [currentView respondsToSelector:@selector(setEnabled:)] )
{
[(NSControl*)currentView setEnabled:enabled];
}
[currentView setSubViewsEnabled:enabled];
[currentView display];
}
}
@end
subclass the NSView
and add following method
-(void)mouseDown:(NSEvent *)theEvent
{
}
Here is an example in Swift 4. Place your views into this custom view.
class UserInterectionDisabledView: NSView {
override func hitTest(_ point: NSPoint) -> NSView? {
return nil
}
}
来源:https://stackoverflow.com/questions/11740638/how-to-disable-user-interaction-in-a-custom-view