使用NSTimer旋转和翻译UIImageView

时间:2013-10-20 19:42:37

标签: ios objective-c core-graphics nstimer cgaffinetransform

所以我正在研究汽车的快速动画,我希望轮胎旋转并向前翻转......但由于某种原因,它们向前滑动但后来我添加了旋转它们的代码,现在它们旋转了它来回跳跃随机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);

}

2 个答案:

答案 0 :(得分:1)

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

  1. 将原点移动到任意点
  2. 进行轮换。
  3. 将原点移回原位。
  4. (以下链接指向所涉及的数学: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);                                              
    

答案 1 :(得分:0)

尝试传递角度,而不管其当前的转换如下:

wheel.transform = CGAffineTransformMakeRotation( spin/180*M_PI );