Objective c 是否存在NSButton重叠解决方案?
我试着把一些按钮与不同的图像或形式,问题是当按钮太近,他们重叠 这个想法是,当我执行单击时,按钮的图像只需要响应,而透明部分被忽略 有什么解决办法吗 好的,在读了一个小时的帖子和苹果文档后,我找到了解决方案 所需: 无边框按钮 NSButton自定义类 按钮需要指向我们的自定义NSButton类 一些代码。。。。 下面是代码,只有当我在按钮图像上执行单击并忽略按钮透明外部的事件/单击时,才会使按钮响应 此代码需要将其放入我们的nsButtonCustomClass.m中的NSButton自定义类中Objective c 是否存在NSButton重叠解决方案?,objective-c,nsbutton,Objective C,Nsbutton,我试着把一些按钮与不同的图像或形式,问题是当按钮太近,他们重叠 这个想法是,当我执行单击时,按钮的图像只需要响应,而透明部分被忽略 有什么解决办法吗 好的,在读了一个小时的帖子和苹果文档后,我找到了解决方案 所需: 无边框按钮 NSButton自定义类 按钮需要指向我们的自定义NSButton类 一些代码。。。。 下面是代码,只有当我在按钮图像上执行单击并忽略按钮透明外部的事件/单击时,才会使按钮响应 此代码需要将其放入我们的nsButtonCustomClass.m中的NSButton自定义类
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
}
- (BOOL)isOpaque{
return NO;
}
- (void)mouseDown:(NSEvent *)event {
NSPoint cursorLocation = [event locationInWindow];
cursorLocation = [self convertPoint:cursorLocation fromView:nil];
// Test if the hit occured in the opaque part of the image. We create a 1x1 rectange at the click point and test for intersection with a non-transparent section of the image.
NSRect cursorLocationRect = { cursorLocation, {1, 1} };
if ( [self.image hitTestRect:cursorLocationRect withImageDestinationRect:self.bounds
context:nil hints:nil flipped:NO] == YES ) {
// If the click missed then we ignore it here.
[super mouseDown:event];
return;
}
}
或者代替鼠标事件
voidmouseDown:NSEvent*事件
我们可以使用hitTest功能:
-(NSView *)hitTest:(NSPoint)aPoint{
NSRect myRect= { [self convertPoint:aPoint fromView:nil] ,1,1};
if([self.image hitTestRect:myRect withImageDestinationRect:self.bounds
context:nil hints:nil flipped:NO]){
return [super hitTest:aPoint];
}
return nil;
}
就这样。这是我的工作
还有一件事:
如果按钮图像因某些奇怪的设计而重叠,则按钮类型需要瞬时改变,如果按钮是瞬时推入的,则按下按钮时可能会导致isOpaque=是效应 是的,这帮助我找到了解决方案,有一些类和它们工作,但它们更完整和复杂。我只需要基本的方法,没有任何功能和额外的。谢谢你的快速评论。