CAShapeLayer驱使我疯狂
我想旋转一个CAShapeLayer围绕它的中心点。我对界限,框架,锚点和中心点感到困惑。CAShapeLayer驱使我疯狂
我在UIView中创建了一个圆形的CAShape,并且想围绕它的中心旋转它。我只包含了一些代码。
函数来创建祝旋转圈:
func drawCompassImage(){
//Compass Bezel Ring
let baseCirclePath = UIBezierPath()
baseCirclePath.addArcWithCenter(CGPoint(x: CGFloat(self.frame.width/2), y: CGFloat(self.frame.width/2)), radius: CGFloat(baseCircleRadius), startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(M_PI_2*3), clockwise: true)
compassImage.path = baseCirclePath.CGPath
compassImage.fillColor = UIColor.clearColor().CGColor;
compassImage.strokeColor = UIColor.whiteColor().CGColor
compassImage.lineDashPattern = [1.0,2.0]
compassImage.lineWidth = 10.0
self.layer.addSublayer(compassImage)
}
函数来创建的红点:
func drawCompassRedDot() {
let compassBall = CAShapeLayer()
let compassBallPath = UIBezierPath()
let compassBallRadius :CGFloat = 5
let xStart: Float = Float(Float(baseCircleRadius) * cos(270 * Float(M_PI)/180)) + Float(self.frame.width/2)
let yStart: Float = Float(Float(baseCircleRadius) * sin(270 * Float(M_PI)/180)) + Float((self.frame.height/2)+0)
compassBallPath.addArcWithCenter(CGPoint(x: CGFloat(xStart), y: CGFloat(yStart)), radius: compassBallRadius, startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(M_PI_2*3), clockwise: true)
compassBall.path = compassBallPath.CGPath
compassBall.fillColor = UIColor.redColor().CGColor;
compassBall.strokeColor = UIColor.redColor().CGColor
compassBall.lineWidth = 1.0
self.compassImage.addSublayer(compassBall)
}
功能旋转:
func animate(){
compassImage.transform = CATransform3DMakeRotation(degree2radian(45), 0, 0, 1)
}
func degree2radian(a:CGFloat)->CGFloat {
let b = CGFloat(M_PI) * a/180
return b
}
我的问题是什么我尝试它总是偏离设置:
这两个圆圈应该有相同的中心。
请帮助这是送我屁股...
我已经完成了。需要在边界和锚点添加正确:
compassImage.frame = bounds
compassImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
因此,代码如下所示:
func drawCompassImage(){
//Compass Bezel Ring
let baseCirclePath = UIBezierPath()
baseCirclePath.addArcWithCenter(CGPoint(x: CGFloat(self.frame.width/2), y: CGFloat(self.frame.width/2)), radius: CGFloat(baseCircleRadius), startAngle: CGFloat(-M_PI_2), endAngle:CGFloat(M_PI_2*3), clockwise: true)
compassImage.path = baseCirclePath.CGPath
compassImage.frame = bounds
compassImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
compassImage.fillColor = UIColor.clearColor().CGColor;
compassImage.strokeColor = UIColor.whiteColor().CGColor
compassImage.lineDashPattern = [1.0,2.0]
compassImage.lineWidth = 10.0
self.layer.addSublayer(compassImage)
}
的完整代码请注意,您的baseCirclePath.addArcWithCenter()的工作原理可能不是因为框架和边界是相同的。该框架是在超视图坐标系统中表达的视图的矩形。所以如果你的观点会被调整,这很可能不再按预期工作。在计算视图或图层中的帧和位置时,请始终使用边界,并且不要假设bounds.origin为0,0。这样,即使你的观点的外部世界会以疯狂的方式歪曲你,它也会继续工作。 – Joride
非常感谢您的更新。那么你会推荐改变这条线吗? - baseCirclePath.addArcWithCenter(CGPoint(x:CGFloat(self.frame.width/2),y:CGFloat(self.frame.width/2)),radius:CGFloat(baseCircleRadius),startAngle:CGFloat(-M_PI_2), endAngle:CGFloat(M_PI_2 * 3),顺时针:true) compassImage.path = baseCirclePath.CGPath – duck1970
是的。 shapeLayer位于其超层/视图中。所以你想使用该层/视图的坐标系。相反,你的观点是想知道你的图层占据的位置,它会要求该图层的框架。不知道你是否已经检查过,但如果没有,请检查:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/Coordinates.html – Joride
你能张贴在那里调用这些功能多一点的情况下,让我和其他人可以帮助你更多? @ duck1970 – Ike10
您好我已经添加了类 – duck1970