问题
I had an app which plays videos from a web server but it just plays in landscape orientation. I want my app to use the accelerometer to play my videos both in landscape and in portrait orientation. I want my video playback feature to look like the youtube app in the iPhone. Can anyone please help me how to do this? thanks
回答1:
For this you do not need the accelerometer. Instead you listen to the notifications from the UIDevice singleton instance that are sent when the orientation changes. In your "application didFinishLaunching withOptions" method, type this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange) name: UIDeviceOrientationDidChangeNotification object: nil];
then create this method to handle the orientation change:
- (void)deviceOrientationDidChange {
int orientation = (int)[[UIDevice currentDevice]orientation];
switch (orientation) {
case UIDeviceOrientationFaceDown:
NSLog(@"UIDeviceOrientationFaceDown" );
// handle orientation
break;
case UIDeviceOrientationFaceUp:
NSLog(@"UIDeviceOrientationFaceUp" );
// handle orientation
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"UIDeviceOrientationLandscapeLeft" );
// handle orientation
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"UIDeviceOrientationLandscapeRight" );
// handle orientation
break;
case UIDeviceOrientationPortrait:
NSLog(@"UIDeviceOrientationPortrait" );
// handle orientation
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"UIDeviceOrientationPortraitUpsideDown" );
// handle orientation
break;
case UIDeviceOrientationUnknown:
NSLog(@"UIDeviceOrientationUnknown" );
// handle orientation
break;
default:
break;
}
}
来源:https://stackoverflow.com/questions/8570221/use-accelerometer-in-my-movieplayer-app