Cocos2D读书笔记中UIAccelerometer加速计案例完成是本文要介绍的剧情,主即使来打探UIAccelerometer加速计的实现。UIAccelerometer加速计是用来检查实验iphone手提式有线电话机在x.y.z轴八个轴上的加速度。要拿走该类调用。
Cocos2D学学笔记之UIAccelerometer加速计是本文要介绍的剧情,以UIAccelerometer加快计为实例,来看内容。UIAccelerometer加快计是用来检查测量检验iphone手提式有线电话机在x.y.z轴七个轴上的加快度。要获得该类调用:
加快计的法力
类型供给要求驾驭手提式有线电话机当前是竖屏依然横屏状态,于是Google了瞬间,归咎如下:
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
主意一:注册手提式有线电话机方向退换的公告UIDeviceOrientationDidChangeNotification
并且,你须要安装它的delegate。
与此同一时间,你要求安装它的delegate。
UIAccelerometer的行使手续
- (void)viewDidLoad { [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(doRotateAction:) name:UIDeviceOrientationDidChangeNotificationobject:nil];}- (void)doRotateAction:(NSNotification*)notification {if([[UIDevicecurrentDevice] orientation]全球彩票平台, ==UIDeviceOrientationPortrait|| [[UIDevicecurrentDevice] orientation] ==UIDeviceOrientationPortraitUpsideDown) {NSLog(@"竖屏"); }elseif([[UIDevicecurrentDevice] orientation] ==UIDeviceOrientationLandscapeLeft|| [[UIDevicecurrentDevice] orientation] ==UIDeviceOrientationLandscapeRight) {NSLog(@"横屏"); }}
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer.delegate = self; accelerometer.updateInterval = 1.0/60.0;
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; accelerometer.delegate = self; accelerometer.updateInterval = 1.0/60.0;
// 得到单例对象
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
// 设置代理
accelerometer.delegate = self;
// 设置采集样品间隔
accelerometer.updateInterval = 1.0/30.0;
// 1分钟采集样品贰拾八次
达成代理方法
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
//acceleration中的x、y、z多个属性分别表示每种轴上的加快度
Notice:在手机张开竖排方向锁按时,上述办法行不通。
信托方法:- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration是意味加快度类。包涵了来自加速计UIAccelerometer的真是数据。它有3个属性的值x、y、z。iphone的加快计扶助最高以每秒100次的频率进行轮询。此时是伍拾四次。
寄托方法:
// 创设运动COO对象
CMMotionManager *mgr = [[CMMotionManager alloc] init];
//推断加快计是或不是可用(最佳推断)
if (mgr.isAccelerometerAvailable){
//加快计可用
}
// 设置采集样品间隔
mgr.accelerometerUpdateInterval = 1.0/30.0;
// 1秒钟采集样品二15次
// 最初采集样品(采样到多少就能够调用handler,handler会在queue中实施)
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue*)queue withHandler:(CMAccelerometerHandler)handler;
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
为理解决纵然在手提式有线电电话机展开竖排方向锁定期,照旧能领悟手机是竖向如故横向的标题,在Stack Overflow网址上找到了有的缓和措施。
1) 应用程序能够透过加速计来检查评定摇摆,如:客商能够因而摇拽iphone擦除绘图。
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration中的UIAcceleration
// 创设运动老董对象
CMMotionManager *mgr = [[CMMotionManager alloc] init];
// 剖断加快计是或不是可用(最佳决断)
if (mgr.isAccelerometerAvailable){
//加快计可用
}
// 初始采集样品
- (void)startAccelerometerUpdates;
// 在急需的时候搜集加快度数据
CMAcceleration acc = mgr.accelerometerData.acceleration;
NSLog(@"%f, %f, %f", acc.x,acc.y,acc.z);
上面二种艺术都是应用加速计。
也能够客户延续摆荡五次iphone,推行一些特种的代码:
是象征加速度类。满含了来自加快计UIAccelerometer的便是数据。它有3个属性的值x、y、z。iphone的加快计辅助最高以每秒玖16回的功能举办轮询。此时是陆拾五回。
监督摇一摇的不二秘诀
主意1:通过深入分析加速计数据来推断是还是不是开展了摇一摇操作(比较复杂)
方式2:iOS自带的Shake监控API(特别轻松)
推断摇一摇的步调:达成3个摇一摇监听方法
- (void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)event/**检查实验到摆荡*/
- (void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)event/**摇晃撤销(被中止)*/
- (void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event/**忽悠甘休*/
加快计的原理
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { static NSInteger shakeCount = 0; static NSDate *shakeStart; NSDate *now = [[NSDate alloc] init]; NSDate *checkDate = [[NSDate alloc] initWithTimeInterval:1.5f sinceDate:shakeStart]; if ([now compare:checkDate] == NSOrderedDescending || shakeStart == nil) { shakeCount = 0; [shakeStart release]; shakeStart = [[NSDate alloc] init]; } [now release]; [checkDate release]; if (fabsf(acceleration.x) > 2.0 || fabsf(acceleration.y) > 2.0 || fabsf(acceleration.z) > 2.0) { shakeCount ; if (shakeCount > 4) { // -- DO Something shakeCount = 0; [shakeStart release]; shakeStart = [[NSDate alloc] init]; } } }
1、应用程序能够通过加速计来检验摇摆,如:客商能够经过摆荡iphone擦除绘图。
accelerometer.delegate = self;
2) 加快计最分布的是当做游戏调控器。在打闹中接纳加快计量调整制目标的移位!在简约情状下,只怕只需得到三个轴的值,乘上某些数灵敏度),然后增添到所决定目的的坐标系中。在纵横交叉的游玩中,因为所确立的大意模型特别真实,所以必得凭借加快计重返的值调度所决定目的的快慢。
也得以客户一连摇荡三次iphone,实施一些特殊的代码:
加速计的法则
本文由全球彩票平台发布于全球彩票平台操作系统,转载请注明出处:【全球彩票平台】Cocos2D学习笔记之UIAccelerometer加