我正在NSButtonCell为自定义渲染创建自定义.
现在,我希望有不同的方面取决于鼠标是否在按钮上方.我怎样才能获得这些信息?
感谢致敬,
我有一个派生NSButtonCell自我画边框的类:
override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
let path = NSBezierPath(bound: frame.insetBy(dx: CGFloat(config.buttonInset), dy: CGFloat(config.buttonInset)), withCorners: corners, withRadius: CGFloat(config.cornerRadius), flip: flipIt)
path.lineWidth = config.borderWidth
if(isEnabled)
{
if(isHighlighted)
{
print("isHighlighted true")
let fillColor: NSColor = colorMap.buttonHighlightColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print("isHighlighted false")
if(showsStateBy.contains(.changeGrayCellMask))
{
print(".changeGrayCellMask")
if(state == .on)
{
print(".on")
let fillColor: NSColor = colorMap.buttonOnColor
let strokeColor: NSColor = colorMap.buttonBorderColor
fillColor.setFill()
strokeColor.setStroke()
path.fill()
path.stroke()
}
else
{
print(".off") …Run Code Online (Sandbox Code Playgroud) 在我的cocoa应用程序中,我需要一个用于NSTableView的自定义NSCell.此NSCell子类包含用于处理单击的自定义NSButtonCell(以及用于文本内容的两个或三个NSTextFieldCells).您将在下面找到我的代码的简化示例.
@implementation TheCustomCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
// various NSTextFieldCells
NSTextFieldCell *titleCell = [[NSTextFieldCell alloc] init];
....
// my custom NSButtonCell
MyButtonCell *warningCell = [[MyButtonCell alloc] init];
[warningCell setTarget:self];
[warningCell setAction:@selector(testButton:)];
[warningCell drawWithFrame:buttonRect inView:controlView];
}
Run Code Online (Sandbox Code Playgroud)
我坚持的问题是:在这个NSCell中使用该按钮(更确切地说:NSButtonCell)以正常工作的最佳/正确方法是什么?"工作"表示:触发指定的操作消息并在单击时显示备用图像.开箱即用,按钮在单击时不执行任何操作.
很难找到关于这个主题的信息/读物.我在网上找到的唯一帖子指出我要实施
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp;
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?实现trackMouse:在我的包含NSCell中?然后将事件转发给NSButtonCell?我本来期望NSButtonCell本身知道在点击它时该做什么(我看到trackMouse:方法更多的是在cunjunction,真正跟踪鼠标移动 - 而不是作为'标准'点击行为的训练轮).但是当它被包含在一个单元格本身时似乎不会这样做......似乎我还没有掌握自定义单元格的大图,但是;-)
如果有人能够根据自己的经验回答这个问题(或指向我的某些教程等),我会很高兴 - 告诉我我是否走在正确的轨道上.
托比,提前谢谢
我一直在阅读有关NSButtons和单元格的Apple文档,我似乎无法理解这两者之间的区别.增加这种复杂性,看起来它们都有很多重叠等方法setTitle:,我不确定应该使用哪些方法.
谁能解释一下基本的区别是什么?
谢谢,
Teja
当使用自定义图像和替代图像创建Cocoa斜角按钮时,我有一种奇怪的行为.在按下状态下,按钮背景变为白色.我将按钮添加为透明窗口(HUD窗口)的子视图.
我正在尝试我所知道的每一项技巧:
NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 30.0, 30.0)];
[closeButton setFrameOrigin:NSMakePoint(0.0, 0.0)];
[closeButton setImagePosition:NSImageOnly];
[closeButton setAction:@selector(closeWindowAction:)];
[closeButton setBordered:NO];
[closeButton setTransparent:NO];
[closeButton setImage:[NSImage imageNamed:@"icon-tclose-off"]];
[closeButton setAlternateImage:[NSImage imageNamed:@"icon-tclose-on"]];
[closeButton setBezelStyle:NSShadowlessSquareBezelStyle];
[closeButton setButtonType:NSMomentaryLightButton];
//[[closeButton cell] setBackgroundColor:[NSColor clearColor]];
[[closeButton cell] setHighlightsBy:NSChangeBackgroundCellMask|NSCellLightsByContents];
//[[closeButton cell] setHighlightsBy:NSContentsCellMask];
//[[closeButton cell] setShowsStateBy:0|NSContentsCellMask];
Run Code Online (Sandbox Code Playgroud)
我也试过了
[closeButton setButtonType:NSMomentaryChangeButton];
[[closeButton cell] setHighlightsBy:NSContentsCellMask];
Run Code Online (Sandbox Code Playgroud)
没有结果.
您可以在附加的屏幕截图中看到错误的行为:
斜角按钮覆盖HUD窗口:

错误的斜角按钮背景:

我有一个NSTableVIew用于多选目的,有两列,第一列是NSButtonCell作为复选框,另一列是标题.
我们的想法是检查要添加到数组后面的项目.
问题是当我单击它们时复选框不会改变它的状态.我试图附加一个IBAction,但发送者de de action是TableView但不是复选框
关于如何实现这种功能的任何想法(或链接)?
我需要创建一个同时带有图像和标题的NSButton,但是我不喜欢可可中的任何标准定位方法。
我决定对按钮单元进行子类化,并覆盖-imageRectForBounds:和-titleRectForBounds:提供我的自定义位置。问题在于该-titleRectForBounds:方法可以正常调用,但-imageRectForBounds:不是。
按钮中的图像正常显示,因此单元格必须有一个框架来绘制图像,我只是不知道它是从哪里获得的。
代码非常简单。目前,我唯一要做的就是继承NSButtonCell并重写这两个方法。然后在IB中,选择一个NSButton并将其单元格类更改为我的自定义按钮单元格。
这是代码:
#import "JSButtonCell.h"
@implementation JSButtonCell
- (NSRect)titleRectForBounds:(NSRect)theRect
{
NSLog(@"Bounds for title");
NSLog(@"%@",NSStringFromRect(theRect));
NSRect titleRect = [super titleRectForBounds:theRect];
NSLog(@"Title rect");
NSLog(@"%@",NSStringFromRect(titleRect));
return titleRect;
}
- (NSRect)imageRectForBounds:(NSRect)theRect
{
NSLog(@"Bounds for image");
NSLog(@"%@",NSStringFromRect(theRect));
NSRect imageRect = [super imageRectForBounds:theRect];
NSLog(@"Image rect");
NSLog(@"%@",NSStringFromRect(imageRect));
imageRect.origin.y -= 20;
return imageRect;
}
@end
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的应用程序,其中有一个可变数组的可变数组,例如 -
NSMutableDictionary *sample6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"title6",@"title",[NSNumber numberWithBool:NO],@"state", nil];
Run Code Online (Sandbox Code Playgroud)
在IB中,我使用NSButtonCell(复选框)创建了一个表视图.
我能够使用以下表格列绑定显示复选框状态(已选中或未选中):
Value - ArrayController.arrangedObjects.state
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它显示了一个带有标题的复选框数组 - "Check",如下面的屏幕截图所示:

现在我的目标是使用绑定显示复选框标题,这样它从相同的可变字典中获取值,从中获取其状态.
我尝试按钮单元格的绑定,但它不起作用:
title - > ArrayController.selection.title
我也试过这个按钮单元的绑定:
title - > ArrayController.arrangedObjects.title
但它没有用,在使用上面的绑定之后看起来像这样:

任何人都可以建议我使用哪个控制器密钥,如果这不是显示标题的正确方法那么这样做的正确方法是什么?
cocoa objective-c nstableview nsarraycontroller nsbuttoncell
我设置NSButtonCell为在一个细胞类型Table Column中的我NSTableView.我实现了一种IBAction方法,但是我无法NSButtonCell在IB中连接它- 电线不想突出显示按钮NSTableView.
如何连接方法NSButtonCell?或者也许有另一种方法来处理其点击事件?
谢谢
如何在界面生成器中为cocoa应用程序创建一个带有正常和突出显示状态图像的按钮?我需要一个像cocoa-touch中的自定义按钮这样的按钮,并提供默认和突出显示状态的图形.同样,它适用于纯mac应用程序而不适用于iPhone.
TIA
我已经实现了一个带有图像的NSButton.
当用户将其悬停时,图像会变为其他内容,然后重新开启.
正常状态:

在悬停时:

我用的代码NSButtonCell是:
界面:
#import <Foundation/Foundation.h>
@interface DKHoverButtonCell : NSButtonCell
{
NSImage *_oldImage;
NSImage *hoverImage;
}
@property (retain) NSImage *hoverImage;
@end
Run Code Online (Sandbox Code Playgroud)
实施:
#import "DKHoverButtonCell.h"
@interface NSButtonCell()
- (void)_updateMouseTracking;
@end
@implementation DKHoverButtonCell
@synthesize hoverImage;
- (void)mouseEntered:(NSEvent *)event {
if (hoverImage != nil && [hoverImage isValid]) {
_oldImage = [[(NSButton *)[self controlView] image] retain];
[(NSButton *)[self controlView] setImage:hoverImage];
}
}
- (void)mouseExited:(NSEvent *)event {
if (_oldImage != nil && [_oldImage isValid]) {
[(NSButton *)[self controlView] setImage:_oldImage];
[_oldImage release]; …Run Code Online (Sandbox Code Playgroud)
我将NSButtonCell类子类化为我的按钮给出了不同的外观.我的绘图代码工作正常.这是一个圆形的按钮,里面装满CGGradients,看起来就像是iTunes播放控件.它们并不完全相同,但它们对我来说足够相似.
现在我的问题是按钮类型.我设置了按钮类型,-[PlayerButtonCell initImageCell:]但我没有让它工作,只按下按钮时绘制推入的外观.我告诉你一段我的代码:
//
// PlayerButtonCell.m
// DownTube
//
#import "PlayerButtonCell.h"
@implementation PlayerButtonCell
/* MARK: Init */
- (id)initImageCell:(NSImage *)image {
self = [super initImageCell:image];
if(self != nil) {
[self setImage:image];
[self setButtonType:NSMomentaryPushInButton];
}
return self;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSImage *myImage;
NSBezierPath *myPath;
NSRect myFrame;
NSInteger myState;
myFrame = NSInsetRect(cellFrame , STROKE_WIDTH / 2.0 , STROKE_WIDTH / 2.0);
myImage = [self image];
myState = [self state];
NSLog(@"%d", [self buttonType]);
/* Create bezier path */
{ …Run Code Online (Sandbox Code Playgroud) nsbuttoncell ×12
cocoa ×11
nsbutton ×7
macos ×4
objective-c ×4
nstableview ×3
click ×1
drawing ×1
hover ×1
macos-mojave ×1
nscell ×1
subclass ×1
swift ×1
tableview ×1
transparency ×1