objective c - Drawing issues after subclassing NSCollectionView -
ok, here's have done:
i havenscollectionview
i wanted able enable "selecting" items, , drawing custom border when items selected i subclassed nscollectionviewitem
(to enable selection) i subclassed nsview
nscollectionviewitem
view, in order draw border the code the view item
@implementation mslibrarycollectionviewitem - (void)setselected:(bool)flag { [super setselected:flag]; [(mslibrarycollectionviewview*)[self view] setselected:flag]; [(mslibrarycollectionviewview*)[self view] setneedsdisplay:yes]; }
the custom view
@implementation mslibrarycollectionviewview /*************************************** initialisation ***************************************/ - (mslibrarycollectionviewview*)initwithframe:(nsrect)frame { self = [super initwithframe:frame]; if (self) { // initialization code here. } homecoming self; } /*************************************** drawing ***************************************/ - (void)drawrect:(nsrect)rect { if ([self selected]) { //[[nscolor redcolor] setfill]; //nsrectfill(rect); //[super drawrect:rect]; nscolor* gs = [nscolor colorwithcalibratedred:0.06 green:0.45 blue:0.86 alpha:1.0]; nscolor* ge = [nscolor colorwithcalibratedred:0.12 green:0.64 blue:0.94 alpha:1.0]; nsgradient* g = [[nsgradient alloc] initwithstartingcolor:ge endingcolor:gs]; nscolor *bordercolor = [nscolor colorfromgradient:g]; nsrect framerect = [self bounds]; if(rect.size.height < framerect.size.height) return; nsrect newrect = nsmakerect(rect.origin.x+5, rect.origin.y+5, rect.size.width-10, rect.size.height-10); nsbezierpath *textviewsurround = [nsbezierpath bezierpathwithroundedrect:newrect xradius:7 yradius:7]; [textviewsurround setlinewidth:2.0]; [bordercolor set]; [textviewsurround stroke]; } }
however, seems wrong drawing. example:
when resizing collection view's container, weird line appears @ outer box when collection view item not 100% visible (e.g. because it's been scrolled down), selection border doesn't appear @ (while expect draw visible portion). some exampleswhat's going on?
p.s. i'm not guru drawing , custom views in cocoa - ideas/help more welcome!
you switched asking collection view talking outline view, assume mental hiccup.
when outline view item not 100% visible (e.g. because it's been scrolled down), selection border doesn't appear @ (while expect draw visible portion).that's because of code in -drawrect:
.
if(rect.size.height < framerect.size.height) return;
it's avoiding drawing partial selection outline.
regarding weird line, uncertainty has collection item view's custom drawing. stop happening if disable custom drawing? experiment using ordinary color rather using third-party +colorfromgradient:
code you're using.
by way, line:
nsrect newrect = nsmakerect(rect.origin.x+5, rect.origin.y+5, rect.size.width-10, rect.size.height-10);
could written more as:
nsrect newrect = nsinsetrect(rect, 5, 5);
objective-c cocoa nsview nscollectionview nscollectionviewitem
No comments:
Post a Comment