使用设备运动管理器的目标C/iOS更新间隔问题

使用设备运动管理器的目标C/iOS更新间隔问题,ios,objective-c,devicemotion,Ios,Objective C,Devicemotion,代码用于获取运动和陀螺仪更新,分别放入阵列中,并在发生捕获事件时选择阵列中的最大值。如果设备运动更新设置为.02或.03,则一切正常;但是,当更新设置为应用程序所需的.01时,我将获得额外的输出/值。任何帮助或指导都将不胜感激。提前谢谢。代码如下:` #import "ViewController.h" #import <CoreMotion/CoreMotion.h> #define kRadToDeg 57.2957795 @interface ViewControlle

代码用于获取运动和陀螺仪更新,分别放入阵列中,并在发生捕获事件时选择阵列中的最大值。如果设备运动更新设置为.02或.03,则一切正常;但是,当更新设置为应用程序所需的.01时,我将获得额外的输出/值。任何帮助或指导都将不胜感激。提前谢谢。代码如下:`

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

#define kRadToDeg   57.2957795

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (weak, nonatomic) IBOutlet UILabel *rotationXLabel;
@property (nonatomic, strong) CMDeviceMotion *motion;
@property (nonatomic, strong) NSMutableArray *pitchArray;
@property (nonatomic, strong) NSMutableArray *rotationArray;
@property (nonatomic) id maxRotation;
@property (nonatomic) id maxPitch;
@property BOOL captureValues;

@end

@implementation ViewController

- (CMMotionManager *)motionManager
{
    if (!_motionManager) {
    _motionManager = [CMMotionManager new];
    [_motionManager setDeviceMotionUpdateInterval:(.01)];
    [_motionManager setGyroUpdateInterval:(.01)];
}
return _motionManager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self placePitchValuesInArray];
[self placeRotationValuesInArray];
}

-(void)placePitchValuesInArray {

NSMutableArray *pitchArray = [NSMutableArray array];
pitchArray = [[NSMutableArray  alloc] initWithCapacity:150];

if (_captureValues == NO) {

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue              currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {

 self.pitchLabel.text = [NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg];

[pitchArray addObject:[NSString stringWithFormat:@"%.2gº", motion.attitude.pitch *  kRadToDeg]];

        _pitchArray = pitchArray;

        if (pitchArray.count >= 150) {  //maintain running array of 150 pitch values

        [pitchArray removeObjectAtIndex:0];
        }
        id maxPitch = [_rotationArray valueForKeyPath:@"@max.intValue"];

        _maxPitch = maxPitch;

         [self checkCapture];

         }];

         }
      }

-(void)placeRotationValuesInArray {

NSMutableArray *rotationArray = [NSMutableArray array];

rotationArray = [[NSMutableArray  alloc] initWithCapacity:150 ];

if (self.captureValues == NO) {

    [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]  withHandler:^(CMGyroData *gyroData, NSError *error) {

        self.rotationXLabel.text = [NSString stringWithFormat:@"%f",  gyroData.rotationRate.x];

        [rotationArray addObject:[NSString stringWithFormat:@"%f", gyroData.rotationRate.x]];

        _rotationArray = rotationArray;

        if (rotationArray.count >= 150) { //maintain running array of 150 rotaion  values

            [rotationArray removeObjectAtIndex:0];
        }

        id maxRotation = [rotationArray valueForKeyPath:@"@max.intValue"];

        _maxRotation = maxRotation;

        [self checkCapture];
        }];

       }
   }

 -(void)checkCapture {

//to generate capture. iPhone is landscape with HOME button on RIGHT. Tilt left side     UP slightly. ROTATE quickly in a counter-clockwise fashion.

if (([self.pitchLabel.text integerValue] > 3) && ([self.rotationXLabel.text   integerValue] > 5))
{
    (_captureValues = YES);
    [_motionManager stopDeviceMotionUpdates];
    [_motionManager stopGyroUpdates];

    for (int i=0; i<30; i++){ // modify pitchArray to remove last 30 values
        [self.pitchArray removeLastObject];

    }

    id maxPitch = [_pitchArray valueForKeyPath:@"@max.integerValue"];

    _maxPitch = maxPitch;

   [self outputValues];
}

}

-(void)outputValues {

NSLog(@"Max Pitch Value from modified array = %@", _maxPitch);
NSLog(@"Max Rotation Value = %@", _maxRotation);
sleep(2.5);
[self resetFlagAndArrays];

}
-(void)resetFlagAndArrays {

(_captureValues = NO);
[_pitchArray removeAllObjects];
[_rotationArray removeAllObjects];
[self viewDidLoad];

 }

 @end
#导入“ViewController.h”
#进口
#定义kRadToDeg 57.2957795
@界面视图控制器()
@属性(弱,非原子)IBUILabel*pitchLabel;
@属性(非原子、强)CMMotionManager*motionManager;
@性质(弱,非原子)IBUILabel*旋转XLabel;
@属性(非原子,强)CMDeviceMotion*运动;
@属性(非原子,强)NSMutableArray*pitchArray;
@属性(非原子,强)NSMutableArray*rotationArray;
@属性(非原子)id maxRotation;
@属性(非原子)id maxPitch;
@资产净值;
@结束
@实现视图控制器
-(CMMotionManager*)运动管理器
{
如果(!\u motionManager){
_motionManager=[CMMotionManager新建];
[_MotionManagerSetDeviceMotionUpdateInterval:(.01)];
[_motionManager设置GyroUpdateInterval:(.01)];
}
返回运动管理器;
}
-(无效)viewDidLoad
{
[超级视图下载];
[self-placePitchValuesInArray];
[阵列中的自放置旋转值];
}
-(无效)placePitchValuesInArray{
NSMutableArray*pitchArray=[NSMutableArray];
pitchArray=[[NSMutableArray alloc]initWithCapacity:150];
如果(_captureValues==否){
[self.motionManager StartDeviceMotion更新到队列:[NSOperationQueue currentQueue]和处理程序:^(CMDeviceMotion*运动,NSError*错误){
self.pitchLabel.text=[NSString stringWithFormat:@“%.2gº”,motion.attitude.pitch*kRadToDeg];
[pitchArray addObject:[NSString stringWithFormat:@“%.2gº”,motion.attiture.pitch*kRadToDeg];
_pitchArray=pitchArray;
如果(pitchArray.count>=150){//保持150个螺距值的运行数组
[pitchArray removeObjectAtIndex:0];
}
id maxPitch=[\u rotationArray valueForKeyPath:@“@max.intValue”];
_maxPitch=maxPitch;
[自我检查捕获];
}];
}
}
-(无效)将旋转值放入数组中{
NSMutableArray*rotationArray=[NSMutableArray];
rotationArray=[[NSMutableArray alloc]initWithCapacity:150];
if(self.captureValues==否){
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]带处理程序:^(CMGyroData*gyroData,NSError*错误){
self.rotationXLabel.text=[NSString stringWithFormat:@“%f”,gyroData.rotationRate.x];
[rotationArray添加对象:[NSString stringWithFormat:@“%f”,gyroData.rotationRate.x]];
_RotationAry=RotationAry;
如果(rotationaray.count>=150){//保持150个旋转值的运行数组
[rotationArray removeObjectAtIndex:0];
}
id maxRotation=[rotationArray valueForKeyPath:@“@max.intValue”];
_最大旋转=最大旋转;
[自我检查捕获];
}];
}
}
-(无效)支票捕获{
//生成捕获。iPhone是横向的,右键为HOME(主页)按钮。稍微向上倾斜左侧。逆时针快速旋转。
如果(([self.pitchLabel.text integerValue]>3)和([self.rotationXLabel.text integerValue]>5))
{
(_captureValues=是);
[_motionManager停止设备运动更新];
[_motionManager停止更新];

for(int i=0;i只是想为任何有类似问题的人共享解决方案。在上面的checkCapture方法中停止设备运动更新后,我添加了一个延迟,并将“for循环”移动到一个单独的方法中-如下所示:

[self performSelector:@selector(adjustValues) withObject:nil afterDelay:.5];

-(void)adjustValues {
 for (int i=0; i<30; i++){ // modify pitchArray to remove last 30 values
    [self.pitchArray removeLastObject];

 }

 id maxPitch = [_pitchArray valueForKeyPath:@"@max.integerValue"];

 _maxPitch = maxPitch;

 [self outputValues];
 }
[自执行选择器:@selector(adjustValues)with object:nil afterDelay:.5];
-(无效)调整值{
对于(int i=0;i