android ios 重力感应器,iOS实时获取当前的屏幕方向之重力感应
相信大部分開發者都知道獲取當前屏幕方向的方法有很多,但是基本上想獲取到都有一定的限制條件,所以比較郁悶,經過作者的測試目前采用重力感覺來獲取是最準確的,當然可能有其他辦法,我沒發現,如果您有什么好的方法麻煩在下面給我留言謝謝
話不多說,直接上代碼
@interface GravityInduction ()
{
NSTimeInterval updateInterval;
}
@property (nonatomic,strong) CMMotionManager *mManager;
@end
@implementation GravityInduction
- (CMMotionManager *)mManager
{
if (!_mManager) {
updateInterval = 1.0/15.0;
_mManager = [[CMMotionManager alloc] init];
}
return _mManager;
}
- (void)startUpdateAccelerometerResult:(void (^)(NSInteger))result
{
if ([self.mManager isAccelerometerAvailable] == YES) {
//回調會一直調用,建議獲取到就調用下面的停止方法,需要再重新開始,當然如果需求是實時不間斷的話可以等離開頁面之后再stop
[self.mManager setAccelerometerUpdateInterval:updateInterval];
[self.mManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
{
double x = accelerometerData.acceleration.x;
double y = accelerometerData.acceleration.y;
if (fabs(y) >= fabs(x))
{
if (y >= 0){
//Down
}
else{
//Portrait
}
}
else
{
if (x >= 0){
//Right
}
else{
//Left
}
}
}];
}
}
- (void)stopUpdate
{
if ([self.mManager isAccelerometerActive] == YES)
{
[self.mManager stopAccelerometerUpdates];
}
}
- (void)dealloc
{
_mManager = nil;
}```
>如果覺得我的文章對您有用,請隨意打賞。您的支持將鼓勵我繼續創作!
總結
以上是生活随笔為你收集整理的android ios 重力感应器,iOS实时获取当前的屏幕方向之重力感应的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机房服务器显示器切换,两台电脑主机,一台
- 下一篇: 局域网中的两台主机共享一台显示器