1

所以我正在制作汽车的快速动画,我希望轮胎旋转并向前平移......但由于某种原因它们向前滑动但后来我添加了旋转它们的代码,现在它们旋转并随机跳跃 x来回定位,这非常令人困惑,我不知道发生了什么..我是否遗漏了所需的框架或其他东西?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    frame = [NSTimer scheduledTimerWithTimeInterval:1/40.f
                                                   target:self
                                                 selector:@selector(enterFrame)
                                                 userInfo:nil
                                                  repeats:YES];


}


-(void)enterFrame {

    //spin += +0.5;
    spin = 0.5;

    //wheel.center = CGPointMake(wheel.center.x+0.5, wheel.center.y);
    wheel.center = CGPointMake(wheel.center.x+spin, wheel.center.y);

    //wheel.transform = CGAffineTransformMakeRotation(spin);        
    wheel.transform = CGAffineTransformRotate(wheel.transform, spin);

}
4

2 回答 2

1

围绕任意点(即不是原点)旋转:

  1. 将原点移动到任意点
  2. 执行旋转。
  3. 将原点移回原始位置。

(以下链接指向所涉及的数学:http ://www.euclideanspace.com/maths/geometry/affine/aroundPoint/matrix2d/ )

试试下面的代码,这可能会解决你的问题:

//translate the origin to the center of the wheel                                           
wheel.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, wheel.center.x, wheel.center.y);   

//rotate the wheel
wheel.transform = CGAffineTransformRotate(wheel.transform, spin);   

//translate the origin back
wheel.transform = CGAffineTransformTranslate(wheel.transform,-wheel.center.x,-wheel.center.y);  

//Move the wheel 
wheel.transform = CGAffineTransformTranslate(wheel.transform,wheel.center.x+spin, wheel.center.y);                                              
于 2013-10-21T11:40:45.957 回答
0

尝试传递角度,而不考虑其当前的转换,例如:

wheel.transform = CGAffineTransformMakeRotation( spin/180*M_PI );
于 2013-10-20T20:03:08.030 回答