swift - Custom Controls for iOS are always offset -
i'm making few custom controls ios. each of these controls draws various layers in drawrect , every single 1 of layers seems offset reason. they're suppose centered in view. here's example of pie chart control:
override public func drawrect(rect: cgrect) { super.drawrect(rect) _valuelayers.foreach { (sublayer : calayer) -> () in sublayer.removefromsuperlayer() } _valuelayers.removeall(keepcapacity: false) let mindim = min(rect.size.width, rect.size.height) let maxslicewidthscale : cgfloat = self.slicewidthscales.count > 0 ? min(self.slicewidthscales.sort(>).first!, cgfloat(1)) : kdefaultslicewidthscale let maxslicewidth = maxslicewidthscale * mindim * 0.5 let center = rect.center() let sum : float = self.values.reduce(0, combine: { $0.floatvalue + $1.floatvalue }).floatvalue let maxradius = (mindim - maxslicewidth) * 0.5 var currentstartangle = self.startangle var x = 0; x < self.values.count; x++ { let ratio = self.values[x].floatvalue / sum let angle = cgfloat(2.0 * m_pi * double(ratio)) let endangle = currentstartangle + angle let slicewidthscale = self.slicewidthscales.count > x ? self.slicewidthscales[x] : kdefaultslicewidthscale let slicewidth = slicewidthscale * mindim * 0.5 var radius = maxradius switch self.slicealignment { case .inner: radius -= 0.5 * (maxslicewidth - slicewidth) case .outer: radius += 0.5 * (maxslicewidth - slicewidth) case .center: radius = maxradius } let arclayer = cashapelayer() arclayer.frame = self.layer.frame arclayer.fillcolor = uicolor.clearcolor().cgcolor arclayer.path = uibezierpath(arccenter: center, radius: radius, startangle: currentstartangle, endangle: endangle, clockwise: self.clockwise).cgpath arclayer.strokecolor = self.colors.count > x ? self.colors[x].cgcolor : kdefaultslicecolor.cgcolor arclayer.linewidth = slicewidth _valuelayers.append(arclayer) self.layer.addsublayer(arclayer) currentstartangle = endangle } if self.showshadow { self.layer.maskstobounds = false self.layer.shadowcolor = self.shadowcolor.cgcolor self.layer.shadowopacity = self.shadowopacity self.layer.shadowoffset = self.shadowoffset self.layer.shadowradius = self.shadowradius } }
i cannot figure out why these controls offset in frame. doing wrong here?
the problem line of code:
arclayer.frame = self.layer.frame
when layer's origin wasn't (0,0), sublayer being offset.
Comments
Post a Comment