加速计(小球dem)

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import "UIView+Extension.h"
#import "AudioTool.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ball;


@property(nonatomic ,strong)CMMotionManager * manager;

//记录小球在X , Y 方向的加速度
@property(nonatomic ,assign)CGPoint speed;

//记录小球的坐标
@property(nonatomic ,assign)CGPoint loc ;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     //创建运动事件管理者
    CMMotionManager * manager = [[CMMotionManager alloc]init];
    
    //使用一个属性来延长manager的生命周期 (否则block无法执行)
    self.manager = manager;
    
    //设置加速计数据更新间隔
    manager.accelerometerUpdateInterval = 1/30;

    //开始监测加速计数据更新
     [manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
         
         
         NSLog(@"%f , %f , %f" , accelerometerData.acceleration.x , accelerometerData.acceleration.y , accelerometerData.acceleration.z);
         
         //我们要通过加速度 算出每一时刻小球的移动距离
         //也就是 要通过  上一时间的位移+加速度 = 此时的位移量
         
         //每一时刻的加速度
        _speed.x += accelerometerData.acceleration.x;
         
         //因为加速计的坐标的Y方向与手机坐标的Y是相反的
         _speed.y -= accelerometerData.acceleration.y;
    
         //更新小球的frame 的x , y 值
         self.ball.x += self.speed.x;
         
         self.ball.y += self.speed.y;
         
         
         //判断小球的坐标, 不让小球移除屏幕范围
         
         if (self.ball.x <= 0 ) {
             
             self.ball.x = 0;
             
             //让小球减速,因为加速一直变大, 小球的速度也就越快,位移也就越大,屏幕就无法捕捉到小球移动了
             
             _speed.x *= -0.5;
         }
         
         if (self.ball.x >= self.view.width - self.ball.width) {
             
             self.ball.x = self.view.width - self.ball.width;
             
             _speed.x *= -0.5;
             
         }
         
         
         if (self.ball.y <= 0) {
             
             self.ball.y = 0;
             
             _speed.y *= -0.5;
         }
         
         if (self.ball.y >= self.view.height - self.ball.height) {
             
             self.ball.y = self.view .height - self.ball .height;
             
             _speed.y *= -0.5;
         }
         
         
         //判断小球是否碰撞到边缘
         if (self.ball.x ==0 || self.ball.x == self.view.width - self.ball.width || self.ball.y == 0 || self.ball.y == self.view.height - self.ball.height) {
             
             
             if (self.ball.x == self.loc.x || self.ball.y == self.loc.y) {
                 
                 
             }else{
   
                 //播放音乐
                  [AudioTool playWithFileName:@"1.aif"];
             }
         }
      
         //记录小球位置
        _loc.x = self.ball.x;
         
        _loc.y = self.ball.y;
         
         
     }];
}

 

以上是CoreMotion框架实现加速计的demo,下面是已经过期的API 实现加速计的代码:

iOS 5.0 推出的UIAccelerometer  后不更新 被CoreMotion 框架替代

•加速计使用步骤

–1. 获取共享加速计对象

–2. 设置加速计数据更新频率

–3. 设置加速计对象代理

 

//检测 手机 三个方向  上的加速度值
    //重力加速度
    //加速计 设备  一个  硬件设备
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    self.accelerometer = accelerometer;
    accelerometer.updateInterval = 1/30.0;
    
    //设置代理
    accelerometer.delegate = self;
    
    
    
}
//更新了 加速度的值 就会调用 参数1  加速计 参数2 返回值
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSLog(@"%f   %f   %f",acceleration.x,acceleration.y,acceleration.z);
    
}

 

转载于:https://www.cnblogs.com/yuwei0911/p/5430181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值