gpt4 book ai didi

iOS swift Xcode 6 : CGAffineTransformRotate with auto-layout anchorpoint

转载 作者:行者123 更新时间:2023-11-28 13:20:27 24 4
gpt4 key购买 nike

我正在制作一个带有可旋转饼图的应用。但是,我的饼图围绕 (0,0) 旋转,我找不到使其围绕其中心旋转的解决方案。

部分代码:

// DRAWING CODE
// Set constants for piePieces
let radius: CGFloat = CGFloat(screen.width * 0.43)
let pi: CGFloat = 3.1415926535
let sliceRad: CGFloat = 2.0 * pi / CGFloat(categoryArray.count)
var currentAngle: CGFloat = -0.5 * sliceRad - 0.5 * pi
let center: CGPoint = CGPoint(x: screen.width / 2.0, y: radius)
println("Center point: \(center)")

//container!.layer.anchorPoint = CGPoint(x: screen.width, y: radius)

// Draw all pie charts, add them to container (chartView)
for category in categoryArray {

let slice = UIView()
slice.frame = self.frame
let shapeLayer = CAShapeLayer()
let scoreIndex = CGFloat(category.calculateScoreIndex())
let sliceRadius: CGFloat = scoreIndex * radius

// Draw the path
var path:UIBezierPath = UIBezierPath()

path.moveToPoint(center)
path.addLineToPoint(CGPoint(x: center.x + sliceRadius * cos(currentAngle), y: center.y + sliceRadius * sin(currentAngle)))
path.addArcWithCenter(center, radius: sliceRadius, startAngle: currentAngle, endAngle: currentAngle + sliceRad, clockwise: true)
path.addLineToPoint(center)
path.closePath()

// For next slice, add 2*pi Rad / n categories
currentAngle += sliceRad

// Add path to shapeLayer
shapeLayer.path = path.CGPath
//shapeLayer.frame = self.frame
shapeLayer.fillColor = SurveyColors().getColor(category.categoryIndex).CGColor
shapeLayer.anchorPoint = center

// Add shapeLayer to sliceView
slice.layer.addSublayer(shapeLayer)

// Add slice to chartView
container!.addSubview(slice)
}

self.addSubview(container!)
//container!.center = center
container!.layer.anchorPoint = center

}

我安排了一个 NSTimer 每 2 秒执行一次旋转(出于测试目的):

func rotate() {
let t: CGAffineTransform = CGAffineTransformRotate(container!.transform, -0.78)
container!.transform = t;
}

饼图围绕(0,0)旋转。出了什么问题?

enter image description here Rotating around 0,0

最佳答案

我相信解决您的问题的一个好方法是设置一个容器 View :

container = UIView()
container!.frame = CGRect(x: 0.0, y: 0.0, width: screen.width, height: screen.width)
container!.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
container!.backgroundColor = UIColor.clearColor()

然后将所有切片添加到这个容器 View 中:

let slice = UIView()
slice.frame = CGRect(x: 0.0 , y: 0.0 , width: container!.bounds.width, height: container!.bounds.height)
let shapeLayer = CAShapeLayer()
let scoreIndex = CGFloat(category.calculateScoreIndex())
let sliceRadius: CGFloat = scoreIndex * radius
/*
*/
container!.addSubview(slice)

此外,请确保不要将图表作为 subview 添加到具有自动布局约束的 View (这可能会干扰您的 CGAffineTransformRotate)。

关于iOS swift Xcode 6 : CGAffineTransformRotate with auto-layout anchorpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366230/

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