Tuesday, 15 May 2012

cocoa - Inherited control does not draw -



cocoa - Inherited control does not draw -

i'm trying create own progress bar subclassing nsprogressindicator. wrote code using playground in xcode 6 , works fine (the content beingness drawn correctly).

as set class onto gui (either type "custom view" or "indeterminate progress indicator") command not draw though drawrect(dirtyrect: nsrect) method has been overridden , beingness called framework.

here's code:

class abprogressbar : nsprogressindicator { allow drawstep = 10 var rounded: bool = true var margin: cgfloat = 4.0 var barcolor: nscolor = nscolor.bluecolor() var barbordercolor: nscolor = nscolor.whitecolor() var bordercolor: nscolor = nscolor.graycolor() var backgroundcolor: nscolor = nscolor.blackcolor() init(coder: nscoder!) { println(__function__) super.init(coder: coder) } init(frame framerect: nsrect) { println("\(__function__) frame \(framerect)") super.init(frame: framerect) } override func drawrect(dirtyrect: nsrect) { println(__function__) // here calculate total value area minvalue maxvalue in order find out percental width of inner bar allow area = minvalue < 0 && maxvalue < 0 ? abs(minvalue) + maxvalue : abs(maxvalue) + abs(minvalue) allow currentpercentagefilled: double = doublevalue >= maxvalue ? maxvalue : 100 / area * doublevalue allow innerwidth = (frame.width - (margin * 2)) / 100 * currentpercentagefilled allow radouterx = rounded ? frame.height / 2 : 0 allow radoutery = rounded ? frame.width / 2 : 0 allow radinnerx = rounded ? (frame.height - margin) / 2 : 0 allow radinnery = rounded ? innerwidth / 2 : 0 // inner frame depends on width filled current value allow innerframe = nsrect(x: frame.origin.x + margin, y: frame.origin.y + margin, width: innerwidth, height: frame.height - (margin * 2)) allow pathouter: nsbezierpath = nsbezierpath(roundedrect: frame, xradius: radouterx, yradius: radoutery) allow pathinner: nsbezierpath = nsbezierpath(roundedrect: innerframe, xradius: radinnerx, yradius: radinnery) allow gradientouter: nsgradient = nsgradient(startingcolor: nscolor.whitecolor(), endingcolor: backgroundcolor) allow gradientinner: nsgradient = nsgradient(startingcolor: nscolor.graycolor(), endingcolor: barcolor) gradientouter.drawinbezierpath(pathouter, angle: 270.0) if(pathinner.elementcount > 0) { gradientinner.drawinbezierpath(pathinner, angle: 270.0) } bordercolor.set() pathouter.stroke() barbordercolor.set() pathinner.stroke() } }

using within playground works fine. setting type command placed on ui not work.

does have clue might wrong?

edit: add together information: checked view using new "debug view hierarchy" feature included in xcode 6. result: command there.

i figured out... problem used frame property of super class instead of dirtyrect parameter passed drawrect(...) method.

i exchanged every access frame dirtyrect , works.

looks bit unusual me because (as far understand it) dirtyrect should refer same rectangle frame.

cocoa swift swift-playground

No comments:

Post a Comment