日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

How to write a custom control with NSControl ...

發(fā)布時(shí)間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 How to write a custom control with NSControl ... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

Generally,when we write a custom control by yourself,you should use "NSControl" or "NSActionCell" to do.

And before the long ago,apple's GUI file is .nib,you can subclass a class from NSCell directly,of course,you should override some method from NSCell,now we don't discuss how to override those methods,but discuss the error that if we do that like that NOW,because?now it use xib,and the Cocoa framework has also been changed.

Okay.If you do that,the old code maybe has no issue,but the code from you NOW?will have error.it will be ?like this:

Feb 19 11:15:01 bogon Multi-Color-ProgressIndicator[1271] <Error>: kCGErrorFailure: CGSShapeWindow

Feb 19 11:15:01 bogon Multi-Color-ProgressIndicator[1271] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.

2013-02-19 11:15:01.465 Multi-Color-ProgressIndicator[1271:303] _NXPlaceWindow: error setting window shape (1000)

Feb 19 11:15:01 bogon Multi-Color-ProgressIndicator[1271] <Error>: kCGErrorFailure: CGSShapeWindow

2013-02-19 11:15:01.465 Multi-Color-ProgressIndicator[1271:303] _NSShapeRoundedWindowWithWeighting: error setting window shape (1000)

Only say "like this",because every sample will be different.And the window will be very big sometimes

So how to fix this issue,the way is to override another method from NSCell/NSActionCell,it is:

- (NSSize)cellSize;

The follow is answer:

The -cellSize method must be overridden in your NSCell/NSActionCell subclass. After gobs of stack tracing I discovered that this method will return (40000, 40000) as your cell size if it is not overridden thus creating the sizing errors that we have seen. Since I have special needs in my NSActionCell subclass that require the cell to occupy the entire NSControl's drawing area I simply used the following.

- (NSSize)cellSize { return self.controlView.bounds.size; }

Now,please let us to explain it using a little demo.

I want a NSProgressIndicator object has two more color,but Cocoa only provide ONE color,black,and you can subclass from?NSProgressIndicator and override the method,- (void) drawRect: (NSRect) rect,but I try many ways to do that,I can not change the color,only change the shape,but it is not my want.

So you can do it like this:

@interface AMIndeterminateProgressIndicatorCell : NSCell {double doubleValue;NSTimeInterval animationDelay;BOOL displayedWhenStopped;BOOL spinning;NSColor *color;float redComponent;float greenComponent;float blueComponent; }- (NSColor *)color; - (void)setColor:(NSColor *)value;- (double)doubleValue; - (void)setDoubleValue:(double)value;- (NSTimeInterval)animationDelay; - (void)setAnimationDelay:(NSTimeInterval)value;- (BOOL)isDisplayedWhenStopped; - (void)setDisplayedWhenStopped:(BOOL)value;- (BOOL)isSpinning; - (void)setSpinning:(BOOL)value;@end@implementation AMIndeterminateProgressIndicatorCell- (id)init {if (self = [super initImageCell:nil]) {[self setAnimationDelay:5.0/60.0];[self setDisplayedWhenStopped:YES];[self setDoubleValue:0.0];[self setColor:[NSColor blueColor]];}return self; }- (void)dealloc {[color release];[super dealloc]; }- (NSColor *)color {return [[color retain] autorelease]; }- (void)setColor:(NSColor *)value {float alphaComponent;if (color != value) {[color release];color = [value retain];[[color colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"] getRed:&redComponent green:&greenComponent blue:&blueComponent alpha:&alphaComponent];NSAssert((alphaComponent > 0.999), @"color must be opaque");} }- (double)doubleValue {return doubleValue; }- (void)setDoubleValue:(double)value {if (doubleValue != value) {doubleValue = value;if (doubleValue > 1.0) {doubleValue = 1.0;} else if (doubleValue < 0.0) {doubleValue = 0.0;}} }- (NSTimeInterval)animationDelay {return animationDelay; }- (void)setAnimationDelay:(NSTimeInterval)value {if (animationDelay != value) {animationDelay = value;} }- (BOOL)isDisplayedWhenStopped {return displayedWhenStopped; }- (void)setDisplayedWhenStopped:(BOOL)value {if (displayedWhenStopped != value) {displayedWhenStopped = value;} }- (BOOL)isSpinning {return spinning; }- (void)setSpinning:(BOOL)value {if (spinning != value) {spinning = value;} }- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {[self drawInteriorWithFrame:cellFrame inView:controlView]; }- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {//...it is too long,if you need,please leave your email }- (void)setObjectValue:(id)value {if ([value respondsToSelector:@selector(boolValue)]) {[self setSpinning:[value boolValue]];} else {[self setSpinning:NO];} }@end Then you can use it now,try it now!

Thanks,

Blues

轉(zhuǎn)載于:https://my.oschina.net/u/913344/blog/109145

總結(jié)

以上是生活随笔為你收集整理的How to write a custom control with NSControl ...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。