网站和 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