0

网站和 Obj C 的新手。尝试从 Device Motion(工作)中获取音高值,将最近的 60 个值(不工作)放入一个数组中,然后选择数组中的最大值。对于来自设备的每个新音高值,新的音高值将添加到数组中,并删除第 61 个值。当我挂上手机并跑步时,我得到了 pitch 和 maxPitch 的日志值;但是,我没有得到 60 个值的数组,所以我不相信它工作正常。任何帮助是极大的赞赏。

我相信问题可能出在这条线上:if (pitchArray.count <= 60) {

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

这是完整的代码:

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

#define kRadToDeg   57.2957795

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;
@property (nonatomic, strong) CMMotionManager *motionManager;

@end

@implementation ViewController

- (CMMotionManager *)motionManager
{
    if (!_motionManager) {
        _motionManager = [CMMotionManager new];
        [_motionManager setDeviceMotionUpdateInterval:1/60];
    }
    return _motionManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

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

        if (pitchArray.count <= 60) {

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

        [pitchArray removeObjectAtIndex:0];

        }

        NSNumber *maxPitch = [pitchArray valueForKeyPath:@"@max.intValue"];

        NSLog(@"%@",pitchArray);

        NSLog(@"Max Pitch Value = %d",[maxPitch intValue]);


      }];
}

 @end
4

2 回答 2

0

啊,简单的错误。它没有循环,所以我将 if/else 语句更改为 while。该代码现在可以工作并输出 60 项数组和最大值。

于 2014-07-12T23:42:06.560 回答
0

每次获得新的音高值时,您都会继续分配一个新数组。因此,您应该将音高数组定义为一个属性,并在您的运动更新处理程序之前分配它。您的代码将是:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, strong) NSMutableArray *pitchArray;

@end

@implementation ViewController

- (CMMotionManager *)motionManager
{
    if (!_motionManager) {
        _motionManager = [CMMotionManager new];
        [_motionManager setDeviceMotionUpdateInterval:1/60];
    }
    return _motionManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pitchArray = [[NSMutableArray  alloc] initWithCapacity:60];

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

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

        if (self.pitchArray.count <= 60) {

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

        [self.pitchArray removeObjectAtIndex:0];

        }

        NSNumber *maxPitch = [self.pitchArray valueForKeyPath:@"@max.intValue"];

        NSLog(@"%@",self.pitchArray);

        NSLog(@"Max Pitch Value = %d",[maxPitch intValue]);


      }];
}
于 2014-07-12T21:39:10.923 回答