MotionManager 陀螺儀實現方式
阿新 • • 發佈:2017-07-21
time current data ble cap ups 場景 影像 val
題外話:
權利的遊戲開播了,引用一句話:
布蘭:一個人如果還把,他還能勇敢麽?
奈德:人在害怕時候的勇敢,才是真的勇敢。
回歸正題:
說下關於 CMMotionManager 這個場景的應用吧,微信小視頻不知道大家用過沒,無論你橫著錄,還是豎著錄,或者反著拿手機錄視頻,都會自動轉成 正常錄視頻的樣子(影像不會翻轉),這種方式主要是通過陀螺儀的方式來實現。大家可以了解一下陀螺儀的聲明方法,來仿照微信做一個騰訊小視頻的錄制功能。
陀螺儀代碼如下:
#import <CoreMotion/CoreMotion.h>
CMMotionManager *motionManager = [[CMMotionManageralloc] init];
NSOperationQueue *queue = [[NSOperationQueuealloc] init]; //[NSOperationQueue currentQueue]
// 加速計
if (motionManager.accelerometerAvailable) {
motionManager.accelerometerUpdateInterval = 1.f;
motionManager.gyroUpdateInterval = 1.f;
[motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
if (error) {
[motionManager stopAccelerometerUpdates];
NSLog(@"error: %@", error);
}
else {
NSLog(@"x--> %f\n y--> %f\n z--> %f\n",
accelerometerData.acceleration.x,
accelerometerData.acceleration.y,
accelerometerData.acceleration.z);
[ self hanldeAccelertionData:accelerometerData.acceleration];
}
}];
}
else {
NSLog(@"This device has no accelerometer");
}
- (void)hanldeAccelertionData:(CMAcceleration)acceleration
{
UIInterfaceOrientation orientationNew;
if (acceleration.x >= 0.75) {
orientationNew = UIInterfaceOrientationLandscapeLeft;
}
elseif (acceleration.x <= -0.75) {
orientationNew = UIInterfaceOrientationLandscapeRight;
}
elseif (acceleration.y <= -0.75) {
orientationNew = UIInterfaceOrientationPortrait;
}
elseif (acceleration.y >= 0.75) {
orientationNew = UIInterfaceOrientationPortraitUpsideDown;
}
else {
// Consider same as last time
return;
}
IDSLOG(@"orientationNew: %ld", (long)orientationNew);
if (orientationNew == orientationLast) {
return;
}
orientationLast = orientationNew;
} 明天我會把 騰訊 微視頻的代碼分享給大家,其中錄制方向使用陀螺儀來實現。
#import <CoreMotion/CoreMotion.h>
CMMotionManager
NSOperationQueue *queue = [[NSOperationQueuealloc] init]; //[NSOperationQueue currentQueue]
// 加速計
if (motionManager.accelerometerAvailable) {
motionManager.accelerometerUpdateInterval = 1.f;
motionManager.gyroUpdateInterval = 1.f;
[motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *
if (error) {
[motionManager stopAccelerometerUpdates];
NSLog(@"error: %@", error);
}
else {
NSLog(@"x--> %f\n y--> %f\n z--> %f\n",
accelerometerData.acceleration.x,
accelerometerData.acceleration.y,
accelerometerData.acceleration.z);
[
}
}];
}
else {
NSLog(@"This device has no accelerometer");
}
- (void)hanldeAccelertionData:(CMAcceleration)acceleration
{
UIInterfaceOrientation orientationNew;
if (acceleration.x >= 0.75) {
orientationNew = UIInterfaceOrientationLandscapeLeft;
}
elseif (acceleration.x <= -0.75) {
orientationNew = UIInterfaceOrientationLandscapeRight;
}
elseif (acceleration.y <= -0.75) {
orientationNew = UIInterfaceOrientationPortrait;
}
elseif (acceleration.y >= 0.75) {
orientationNew = UIInterfaceOrientationPortraitUpsideDown;
}
else {
// Consider same as last time
return;
}
IDSLOG(@"orientationNew: %ld", (long)orientationNew);
if (orientationNew == orientationLast) {
return;
}
orientationLast = orientationNew;
} 明天我會把 騰訊 微視頻的代碼分享給大家,其中錄制方向使用陀螺儀來實現。
MotionManager 陀螺儀實現方式