2011-08-26 36 views

回答

0
@interface FirstViewController : UIViewController <UIAccelerometerDelegate> { 
    IBOutlet UIImageView *imageView; 
    CGPoint delta; 
    CGPoint translation; 
    float ballRadius; 
} 

//implementation 

- (void)viewDidLoad 
{  
    [super viewDidLoad]; 

    // for the line in the middle of the camera 
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; 
    accel.delegate = self; 
    accel.updateInterval = 1.0f/60.0f;   
} 

- (void)accelerometer:(UIAccelerometer *)acel 
     didAccelerate:(UIAcceleration *)acceleration { 

    // Get the current device angle 
    float xx = -[acceleration x]; 
    float yy = [acceleration y]; 
    float angle = atan2(yy, xx); 

    // Add 1.5 to the angle to keep the image constantly horizontal to the viewer. 
    [imageView setTransform:CGAffineTransformMakeRotation(angle+1.5)]; 

} 
+0

感谢张贴一个答案!虽然代码片段可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等。 – j0k