开发者

How would I get a UIImageView to always face up based off the accelerometer?

I 开发者_JS百科would like to make it so when the user rotates the device (to any angle, not just landscape/portrait) the UIImageView would always be facing upwards. How would I do this?

Thanks in advance


@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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜