Ios 更新CALayer的旋转
我正在尝试更新CALayer的当前旋转(有时是位置) 我试图通过两个简单的步骤:Ios 更新CALayer的旋转,ios,swift,core-animation,Ios,Swift,Core Animation,我正在尝试更新CALayer的当前旋转(有时是位置) 我试图通过两个简单的步骤: 在一个数组中存储两个calayer,这样我就可以重用它们了 将所有Calayer的锚点设置为0,0 在对象从圆上的某个位置开始的位置绘制CALayer对象 层旋转的角度与该位置的圆旋转的角度相同 更新CALayer的位置和旋转以匹配新值 下面是我的一段代码: lineWidth是一行的宽度 self.items是包含CALayer对象的数组 func updateLines() { var space =
calayer
,这样我就可以重用它们了CALayer
对象lineWidth
是一行的宽度self.items
是包含CALayer
对象的数组
func updateLines() {
var space = 2 * M_PI * Double(circleRadius);
var spaceAvailable = space / (lineWidth)
var visibleItems = [Int]();
var startIndex = items.count - Int(spaceAvailable);
if (startIndex < 0) {
startIndex = 0;
}
for (var i = startIndex; i < self.items.count; i++) {
visibleItems.append(self.items[i]);
}
var circleCenter = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
/* Each line should move up and rotate accordin to this value */
var anglePerLine: CGFloat = (360 / CGFloat(visibleItems.count)).toRadians()
/* Starting position, 270 degrees is on top */
var startAngle: CGFloat = CGFloat(270).toRadians();
/* Lines default rotation, we rotate it to get the right side up */
var lineAngle: CGFloat = CGFloat(180).toRadians();
for (var itemIndex = 0; itemIndex < visibleItems.count; itemIndex++) {
var itemLayer = self.itemLayers[itemIndex];
itemLayer.opacity = 1 - ((0.9 / visibleItems.count) * itemIndex);
/* Calculate start position of layer */
var x = CGFloat(circleRadius) * cos(startAngle) + CGFloat(circleCenter.x);
var y = CGFloat(circleRadius) * sin(startAngle) + CGFloat(circleCenter.y);
var height = CGFloat((arc4random() % 80) + 10);
/* Set position and frame of layer */
itemLayer.frame = CGRectMake(CGFloat(x), CGFloat(y), CGFloat(lineWidth), height);
itemLayer.position = CGPointMake(CGFloat(x), CGFloat(y));
var currentRotation = CGFloat((itemLayer.valueForKeyPath("transform.rotation.z") as NSNumber).floatValue);
var newRotation = lineAngle - currentRotation;
var rotationTransform = CATransform3DRotate(itemLayer.transform, CGFloat(newRotation), 0, 0, 1);
itemLayer.transform = rotationTransform;
lineAngle += anglePerLine;
startAngle += anglePerLine;
}
}
func updateLines(){
var空间=2*M_PI*Double(circleRadius);
var spaceAvailable=空间/(线宽)
var visibleItems=[Int]();
var startIndex=items.count-Int(可用空间);
如果(起始索引<0){
startIndex=0;
}
对于(var i=startIndex;i
第一次运行的结果完全符合我的要求:
第二次运行这段代码只是没有正确更新CALayers,它开始如下所示:
我认为更新CALayer的
位置
和变换
属性与我的代码有关,但无论我做什么,都会导致最后一张图片。通过Twitter回答:设置帧和变换是相互排斥的。很乐意帮忙。查找SO的登录凭据比较困难:D在Twitter上通过@iosengineer找到了答案。在CALayer上设置位置
时,您不希望更新层的帧
,但希望更新边界
Ahhh!!你做的很棒。一个愚蠢的建议——你可以使用图像并设置它们的动画,使它们看起来像旋转一样。它实际上是一个带有随机长度线的图形。我想重用我已经在那里的CALayers,以便能够为它们制作动画。哈哈。看起来像是我不想拿的东西。