如何围绕其自身边界的中心旋转 UIBezierPath?
How to rotate UIBezierPath around center of its own bounds?
假设我们有一个 UIBezierPath...它的边界是完全正方形的...像这样:
func getExponentPath(rotate180: Bool) -> UIBezierPath {
// establish unit of measure (grid) based on this containing view's bounds... (not to be confused with this bezierpath's bounds)
let G = bounds.width / 5
let exponentPath = UIBezierPath()
let sstartPoint = CGPoint(x:(3.8)*G,y:(1.2)*G)
exponentPath.move(to: sstartPoint)
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(1.2)*G))
exponentPath.addLine(to: CGPoint(x:(4.4)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(0)*G))
exponentPath.addLine(to: CGPoint(x:(3.8)*G,y:(0)*G))
exponentPath.addLine(to: CGPoint(x:(3.8)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(4.4)*G,y:(0.2)*G))
exponentPath.addLine(to: sstartPoint)
exponentPath.close()
// this does not work:
// if rotate180 { exponentPath.apply(CGAffineTransform(rotationAngle: CGFloat.pi)) }
return exponentPath
}
如果旋转,此 bezierpath 仍需要在其包含视图中占据完全相同的区域。
我只能假设这是行不通的,因为旋转中心存在一些问题,不是我想要的...尽管即使说 "rotate by 0."[=11 我也会得到相同(错误)的结果=]
那么路径如何围绕它自己的中心点旋转?
似乎应该有一个简单的线性代数矩阵乘法类型可以应用于点集。 =T
我认为您不需要轮换。要绘制相同的倒置形状,只需将其翻转即可:
exponentPath.apply(CGAffineTransform(scaleX: 1, y: -1))
exponentPath.apply(CGAffineTransform(translationX: 0, y: G))
所以如果其他人试图在它自己的边界矩形的中心旋转 UIBezierPath...这是在之前 answers/comments:
的帮助下得出的实际工作解决方案
func getExponentPath(rotationAngle: CGFloat) -> UIBezierPath {
// ...
let x_translation = -( (bounds.width) - ( exponentPath.bounds.width/2) )
let y_translation = -exponentPath.bounds.height/2
exponentPath.apply(CGAffineTransform(translationX: x_translation, y: y_translation))
exponentPath.apply(CGAffineTransform(rotationAngle: rotationAngle))
exponentPath.apply(CGAffineTransform(translationX: -x_translation, y: -y_translation))
// ...
}
extension UIBezierPath
{
func rotateAroundCenter(angle: CGFloat)
{
let center = self.bounds.getCenter()
var transform = CGAffineTransform.identity
transform = transform.translatedBy(x: center.x, y: center.y)
transform = transform.rotated(by: angle)
transform = transform.translatedBy(x: -center.x, y: -center.y)
self.apply(transform)
}
}
假设我们有一个 UIBezierPath...它的边界是完全正方形的...像这样:
func getExponentPath(rotate180: Bool) -> UIBezierPath {
// establish unit of measure (grid) based on this containing view's bounds... (not to be confused with this bezierpath's bounds)
let G = bounds.width / 5
let exponentPath = UIBezierPath()
let sstartPoint = CGPoint(x:(3.8)*G,y:(1.2)*G)
exponentPath.move(to: sstartPoint)
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(1.2)*G))
exponentPath.addLine(to: CGPoint(x:(4.4)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(5)*G,y:(0)*G))
exponentPath.addLine(to: CGPoint(x:(3.8)*G,y:(0)*G))
exponentPath.addLine(to: CGPoint(x:(3.8)*G,y:(0.2)*G))
exponentPath.addLine(to: CGPoint(x:(4.4)*G,y:(0.2)*G))
exponentPath.addLine(to: sstartPoint)
exponentPath.close()
// this does not work:
// if rotate180 { exponentPath.apply(CGAffineTransform(rotationAngle: CGFloat.pi)) }
return exponentPath
}
如果旋转,此 bezierpath 仍需要在其包含视图中占据完全相同的区域。
我只能假设这是行不通的,因为旋转中心存在一些问题,不是我想要的...尽管即使说 "rotate by 0."[=11 我也会得到相同(错误)的结果=]
那么路径如何围绕它自己的中心点旋转?
似乎应该有一个简单的线性代数矩阵乘法类型可以应用于点集。 =T
我认为您不需要轮换。要绘制相同的倒置形状,只需将其翻转即可:
exponentPath.apply(CGAffineTransform(scaleX: 1, y: -1))
exponentPath.apply(CGAffineTransform(translationX: 0, y: G))
所以如果其他人试图在它自己的边界矩形的中心旋转 UIBezierPath...这是在之前 answers/comments:
的帮助下得出的实际工作解决方案func getExponentPath(rotationAngle: CGFloat) -> UIBezierPath {
// ...
let x_translation = -( (bounds.width) - ( exponentPath.bounds.width/2) )
let y_translation = -exponentPath.bounds.height/2
exponentPath.apply(CGAffineTransform(translationX: x_translation, y: y_translation))
exponentPath.apply(CGAffineTransform(rotationAngle: rotationAngle))
exponentPath.apply(CGAffineTransform(translationX: -x_translation, y: -y_translation))
// ...
}
extension UIBezierPath
{
func rotateAroundCenter(angle: CGFloat)
{
let center = self.bounds.getCenter()
var transform = CGAffineTransform.identity
transform = transform.translatedBy(x: center.x, y: center.y)
transform = transform.rotated(by: angle)
transform = transform.translatedBy(x: -center.x, y: -center.y)
self.apply(transform)
}
}