1
UIControlのサブクラスである描画矩形関数で円の進行があります。 Xcodeの8.@IBDesignable UiView描画(_ rect:CGRect)はIBでは動作しますが、デバイスでは動作しません
、3スウィフトそれは次のようになります。
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let ctx = UIGraphicsGetCurrentContext() else {
return
}
let center = CGPoint(x:rect.size.width/2.0,y:rect.size.height/2.0)
ctx.setStrokeColor(UIColor.cyan.cgColor)
ctx.setLineWidth(40)
ctx.setLineCap(.round)
ctx.addArc(center: center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: true)
ctx.drawPath(using: .stroke)
}
このビューは、プログラムに追加されている@IBDesignable UIViewSubclass私が間違っている
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.yellow
self.circleTest = TestCircle(frame: self.bounds)
self.addSubview(circleTest!)
setupConstraints() //pins subview to edges of self.
}
?
ここで、 'radius'はどこに設定されていますか? – Rob
私は、ビューのデバッガの組み合わせを提案したいと思います(サークルビューはどこにあるべきなのか確かめてください)、デバッガ/ロギングをしてください( 'rect'と' center'と 'radius'をあなたが考えるもの彼らはする必要があります)。それは制約の間違いかもしれません。それは '半径'問題かもしれません。提供された情報に基づいて判断するのは難しいです。 – Rob
さて、円のサイズ/中心を推測するのに 'rect'パラメータを使うのはちょっと危険です。代わりに 'bounds'を参照することをお勧めします。 [documentation says](https://developer.apple.com/reference/uikit/uiview/1622529-draw): 'rect'は、ビューの境界のうち更新が必要な部分です。この矩形は通常、ビューの可視範囲全体ですが、その後の描画操作では、矩形はビューの一部のみを指定することがあります。 – Rob