0

我有一个 NSObject 球,我想朝你倾斜 iPhone 的方向移动。我以前从来没有做过任何倾斜的事情,只是触摸。我还希望它以更陡的角度倾斜时移动得更快。谁能帮我?

4

1 回答 1

0

那是我使用的代码,尽管结果很差:您可能想对其进行测试并检查可以改进的地方:

NSDate * lastDate=nil;

-(void)didRotate:(CMRotationRate)rotationRate{
NSDate *newDate=[NSDate date];
CGPoint testCenter=[[[OverlayViewController sharedOverlay] testElement] center];
if (lastDate!=nil){
    NSLog(@"rotate x %f y %f, z %f", rotationRate.x, rotationRate.y, rotationRate.z);
    NSTimeInterval interval=[newDate timeIntervalSinceDate:lastDate];
    CGRect topFrame=[[[OverlayViewController sharedOverlay] view] frame];
    float xAtan=atan(rotationRate.x);
    float yAtan=atan(rotationRate.y);

    float xRotation= topFrame.size.width*xAtan/interval;
    float yRotation=topFrame.size.height*yAtan/interval;
    NSLog(@"topHeight=%f yAtan=%f interval=%f yRotation=%f", topFrame.size.height, yAtan, interval, yRotation);
    NSLog(@"interval %f rotation (%f,%f)", interval, xRotation, yRotation);
    CGPoint newCenter=CGPointMake(testCenter.x+xRotation, testCenter.y+yRotation);
    NSLog(@"old center (%f,%f) new center (%f,%f)", testCenter.x, testCenter.y, newCenter.x, newCenter.y);
    [[[OverlayViewController sharedOverlay] testElement] setCenter:newCenter];
}
lastDate=newDate;
}
于 2014-09-06T18:09:09.017 回答