How to write a custom control with NSControl ...
2019獨角獸企業重金招聘Python工程師標準>>>
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 sometimesSo 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
轉載于:https://my.oschina.net/u/913344/blog/109145
總結
以上是生活随笔為你收集整理的How to write a custom control with NSControl ...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rank,dense_rank,row_
- 下一篇: UML图入门学习笔记