大佬教程收集整理的这篇文章主要介绍了(iOS)Accelerometer Graph(将g-force转换为/ -128)粒度,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
下图显示标签中的x,y,z值与图表上的输出不匹配:(请注意,addX:y:z值是图表上方标签中显示的值)
视图控制器
从蓝牙外设接收x,z值,然后使用以下转换:
// updates LABELS - (void)didReceiveRawAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z { dispatch_async(dispatch_get_main_queue(),^{ _labelAccel.text = [NSString StringWithFormat:@"x:%li y:%li z:%li",(long)x,(long)y,(long)z]; }); } // updates GRAPHS - (void)didReceiveAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z { dispatch_async(dispatch_get_main_queue(),^{ float xx = ((float)X) / 8192; float yy = ((float)y) / 8192; float zz = ((float)z) / 8192; [_xGraph addX:xx y:0 z:0]; [_yGraph addX:0 y:yy z:0]; [_zGraph addX:0 y:0 z:zz]; }); }
GraphView
- (BOOL)addX:(UIAccelerationvalue)x y:(UIAccelerationvalue)y z:(UIAccelerationvalue)z { // If this segment is not full,then we add a new acceleration value to the history. if (index > 0) { // First decrement,both to get to a zero-based index and to flag one fewer position left --index; xhistorY[index] = x; yhistorY[index] = y; zhistorY[index] = z; // And inform Core Animation to redraw the layer. [layer setNeedsDisplay]; } // And return if we are Now full or not (really just avoids needing to call isFull after adding a value). return index == 0; } - (void)drawLayer:(CALayer*)l inContext:(CGContextRef)context { // Fill in the BACkground CGContextSetFillColorWithColor(context,kUIColorLightGray(1.f).CGColor); CGContextFillRect(context,layer.bounds); // Draw the grid lines DrawGridlines(context,0.0,32.0); // Draw the graph CGPoint lines[64]; int i; float _granularity = 16.f; // 16 NSInteger _granualCount = 32; // 32 // X for (i = 0; i < _granualCount; ++i) { lines[i*2].x = i; lines[i*2+1].x = i + 1; lines[i*2].y = xhistorY[i] * _granularity; lines[i*2+1].y = xhistorY[i+1] * _granularity; } CGContextSetstrokeColorWithColor(context,_xColor.CGColor); CGContextstrokeLinesegments(context,lines,64); // Y for (i = 0; i < _granualCount; ++i) { lines[i*2].y = yhistorY[i] * _granularity; lines[i*2+1].y = yhistorY[i+1] * _granularity; } CGContextSetstrokeColorWithColor(context,_yColor.CGColor); CGContextstrokeLinesegments(context,64); // Z for (i = 0; i < _granualCount; ++i) { lines[i*2].y = zhistorY[i] * _granularity; lines[i*2+1].y = zhistorY[i+1] * _granularity; } CGContextSetstrokeColorWithColor(context,_zColor.CGColor); CGContextstrokeLinesegments(context,64); }
您的问题仍然不包括真正重要的内容.我假设xx / yy / zz的计算没有问题.虽然我不知道8192应该是什么意思.
我想这个问题出现在你将值映射到像素坐标的部分……
lines []包含您的值,范围是标签中值的1/8192.所以你的x值-2应该在-0.0000像素的像素位置,所以稍微(远小于1像素)在视图上方…因为你看到线条进一步向下,必须有一些翻译到位(没有在你的代码中显示)
重要但未显示的第二部分是DrawGridlines.可能有一种不同的方法将值映射到像素坐标…
使用调试器检查绘制127行时获得的像素坐标,以及在历史数组中插入值127时获得的像素坐标
在阅读代码时需要改进一些想法:1.)将图形放在它自己的绘制一个图形的类中(并且只有一个历史.不知何故,你似乎已经部分已经存在(否则我无法弄清楚你的_xGraph / _yGraph / _zGraph)但另一方面你绘制所有一个drawLayer中的3个值???目前你似乎有3 * 3历史缓冲区,其中3 * 2填充零…2.)使用一个地方进行Y的计算,用于绘制网格和绘制线条……3.)使用CGContextMoveToPoint(); CGContextAddLineToPoint();而不是用这些丑陋的2 * i 1个版本复制到行[] …
以上是大佬教程为你收集整理的(iOS)Accelerometer Graph(将g-force转换为/ -128)粒度全部内容,希望文章能够帮你解决(iOS)Accelerometer Graph(将g-force转换为/ -128)粒度所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。