Je fais quelques contrôles personnalisés pour iOS. Chacune de ces commandes dessine différentes couches dans drawRect et chaque couche semble être décalée pour une raison quelconque. Ils sont supposés être centrés dans la vue. Voici un exemple d'un contrôle de diagramme circulaire:Les contrôles personnalisés pour iOS sont toujours décalés
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
for 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
}
}
Je ne peux pas comprendre pourquoi ces contrôles sont décalés dans le cadre. Est-ce que je fais quelque chose de mal ici?