0
这是从我的自定义类中提取的,即绘制一些图形。功能drawTick()
增加CAShapeLayer
用蓝色长方形我GameTimer
UIView
:无法更改AnchorPoint for CAShapeLayer iOS Swift
class GameTimer: UIView {
var tick = CAShapeLayer()
...
init(){
super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 200)
}
...
func drawTick() {
tick.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 50, height: 50)).cgPath
tick.fillColor = UIColor.blue.cgColor
self.layer.addSublayer(tick)
tick.anchorPoint = CGPoint(x: 0.5, y: 0.5)
tick.position = CGPoint(x: bounds.midX, y: bounds.midY)
tick.transform = CATransform3DMakeRotation(0.degreesToRadians, 0, 0, 1)
}
...
}
extension Double {
var degreesToRadians : CGFloat {
return CGFloat(self) * CGFloat(M_PI)/180.0
}
}
通过
tick.transform = CATransform3DMakeRotation(45.degreesToRadians, 0, 0, 1)
旋转角度为45度的变迁给下一个结果:
在这里我看到,声明的锚点不会影响tick
图层 - 它应该位于蓝色矩形的中心。旋转绕坐标(x:0,y:0)旋转 - 蓝色矩形的左上角 - tick.position
。 这个问题 - 为什么宣称anchorPoint
的位置不适用于tick
层?这里的行为很奇怪,假设anchorPoint
的默认位置应该已经是(x: 0.5, y: 0.5)
而没有任何手动覆盖,但是如果我删除这个覆盖 - 没有任何更改。
谢谢!正如你所提到的那样工作。 –