UIView
のカスタムを作成しています。draw(rect:)
の方法は、UIBezierPathを使用して大きな幅の円を描くことで、上に四角形を描画します(図のように、ドン色や大きさを考慮しない)。次に、 "設定"アイコン(図2、外側のリングのみを考慮)と一致するように、四角形の回転コピーを作成します。その最後のことを行うには、CGAffineTransform(rotationAngle:)
を使って正方形を回転させる必要がありますが、この回転の中心が円の中心ではなくフレームの原点であるという問題があります。ビュー内の特定のポイントを中心に回転を作成するにはどうすればよいですか?UIBezierPath UIViewの中心を中心に回転
0
A
答えて
2
の変換を適用するの起源、ここに円の中心の周りに歯車の歯を回転させるためにCGAffineTransform
を使用する歯車の図があります:
class Gear: UIView {
var lineWidth: CGFloat = 16
let boxWidth: CGFloat = 20
let toothAngle: CGFloat = 45
override func draw(_ rect: CGRect) {
let radius = (min(bounds.width, bounds.height) - lineWidth)/4.0
var path = UIBezierPath()
path.lineWidth = lineWidth
UIColor.white.set()
// Draw circle
path.move(to: CGPoint(x: center.x + radius, y: center.y))
path.addArc(withCenter: CGPoint(x: center.x, y: center.y), radius: radius, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
path.stroke()
// Box for gear tooth
path = UIBezierPath()
let point = CGPoint(x: center.x - boxWidth/2.0, y: center.y - radius)
path.move(to: point)
path.addLine(to: CGPoint(x: point.x, y: point.y - boxWidth))
path.addLine(to: CGPoint(x: point.x + boxWidth, y: point.y - boxWidth))
path.addLine(to: CGPoint(x: point.x + boxWidth, y: point.y))
path.close()
UIColor.red.set()
// Draw a tooth every toothAngle degrees
for _ in stride(from: toothAngle, through: 360, by: toothAngle) {
// Move origin to center of the circle
path.apply(CGAffineTransform(translationX: -center.x, y: -center.y))
// Rotate
path.apply(CGAffineTransform(rotationAngle: toothAngle * .pi/180))
// Move origin back to original location
path.apply(CGAffineTransform(translationX: center.x, y: center.y))
// Draw the tooth
path.fill()
}
}
}
let view = Gear(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
ここでは、プレイグラウンドで実行されている:
2
シフトトランスフォーム、 回転、 シフトが戻っ
はあなたが(アップ投票)@ DuncanCの答えのデモンストレーションとして
関連する問題
- 1. UIViewの回転中心
- 2. 中心の回転中心を
- 3. OpenGL C++回転中心のオブジェクトを中心に回転する
- 4. html5の中心ピボットを中心にコンテンツを回転する
- 5. Javafxの中心を中心にオブジェクトを回転する
- 6. オブジェクトの中心を中心に回転cammera
- 7. 中心点を中心にCGPointを回転する
- 8. Qt中心点を中心にテキストを回転する
- 9. スウィフト:中心点を中心にレイヤーを回転する
- 10. 中心を中心に回転する矩形
- 11. 空のオブジェクトを中心に回転
- 12. 矩形を中心に回転する
- 13. Mayavi:y軸を中心に回転
- 14. VB.NET中心にグラフィックオブジェクトを回転
- 15. 楕円の中心を中心にArcSegmentを回転する方法
- 16. 重なりのない中心点を中心に三角を回転する
- 17. VTKでのシーンの回転中心
- 18. Velocity.js回転中心軸の周り
- 19. d3.jsで中心を中心にオブジェクトを回転させる方法
- 20. WPFでコラージュを作成 - 固定位置、中心を中心に回転
- 21. MATLAB - 中心点を中心に正三角形を回転します
- 22. アニメーション中のUIViewの中心位置
- 23. 処理中の図形の回転中心を変更する
- 24. 中心点を中心に体を回す
- 25. UITableViewControllerの中心にあるUIView
- 26. 指のタッチで自動的に中心点を中心にイメージを回転する方法
- 27. Three.js - 特定の軸を中心に球を回転する
- 28. pythonで別の点を中心に点を回転
- 29. 任意の点を中心にosmdroid地図を回転する
- 30. OpenGLの特定の点を中心に回転するオブジェクト