【发布时间】:2014-01-21 20:04:30
【问题描述】:
我一直在寻找如何实现幸运型轮子(运行良好)的解决方案,除了我需要轮子外围的图像/标签保持水平而不随轮子旋转.我实现了以下内容,但标签(在本例中为红色椭圆)没有保持水平。
我附上了我在屏幕上构建红色椭圆的代码:
CGFloat cita = 0;
for(int i = 1; i < 2; ++i)
{
CGFloat smallCircleRadius = bigCircleRadius / 8.0;
for (int i = 0; i < 8; i++)
{
CGPoint smallCircleCenter = CGPointMake(wheelCenter.x + bigCircleRadius * cos(cita) - smallCircleRadius/2.0 , wheelCenter.y + bigCircleRadius * sin(cita) - smallCircleRadius / 2.0 );
CGRect smallCircleRect = CGRectMake(smallCircleCenter.x,smallCircleCenter.y,smallCircleRadius * 2,smallCircleRadius);
cita += M_PI / 4.0;
CAShapeLayer *l = [CAShapeLayer layer];
UIBezierPath * p1 = [UIBezierPath bezierPathWithOvalInRect:smallCircleRect];
l.path = p1.CGPath;
l.strokeColor = [[UIColor redColor] CGColor];
l.fillColor = [[UIColor redColor] CGColor];
l.lineWidth = 3.0;
l.anchorPoint = CGPointMake(.5, .5);
[self.emoticonsArray addObject:l];
[self.baseWheel.layer addSublayer:l];
}
}
下面是转动轮子的函数;我正在编写为标签进行旋转的代码行——我显然在这里做错了,但我不知道是什么。非常感谢任何指导。
-(void)spin:(double)delta
{
currentAngle = currentAngle + delta;
CATransform3D transform = CATransform3DMakeRotation(currentAngle, 0, 0, 1);
[self.baseWheel.layer setTransform:transform];
// rotate the red labels here.
for (CAShapeLayer * l in self.emoticonsArray)
{
CGPoint miniWheelCenter = [l convertPoint:l.position toLayer:self.baseWheel.layer.superlayer];
// !! something wrong here!! but what?
CATransform3D t_l = CATransform3DMakeRotation(-currentAngle, miniWheelCenter.x/2, miniWheelCenter.y/2, 1);
[l setTransform:t_l];
}
}
【问题讨论】:
-
问题是因为你正在旋转那些标签是子视图的整个轮子。所以随着轮子它们也会旋转。
-
这是预期的行为。我想做的是让红色椭圆在它们后面跟着轮子移动,同时保持它们的方向不变——这意味着它们应该像现在一样保持水平方向,同时像摩天轮一样绕着圆圈旋转。跨度>
标签: ios animation image-rotation catransform3drotate