gpt4 book ai didi

ios - CAShapeLayer 让我发疯

转载 作者:行者123 更新时间:2023-11-29 00:54:30 31 4
gpt4 key购买 nike

我正在尝试围绕其中心点旋转 CAShapeLayer。我对边界、框架、 anchor 和中心点感到非常困惑。

我在 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
}

我的问题是,无论我尝试什么,它总是以偏移结束:

enter image description here

两个圆应具有相同的圆心。

请帮忙,这让我很疯狂......

最佳答案

我已经解决了。需要正确添加边界和 anchor :

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)

}

关于ios - CAShapeLayer 让我发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767336/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com