重力感应 加速计- By严焕培

//  加速计-传统用法

//

//  Created by 严焕培 on 15-05-19.

//  Copyright (c) 2015年 sibu. All rights reserved.

//

#import "MainViewController.h"

#import <QuartzCore/QuartzCore.h>

@interface MainViewController () <UIAccelerometerDelegate>

{

    // 小球图像

    UIImageView     *_ball;

    // 小球速度

    CGPoint         _ballVelocity;

    // 游戏时钟

    CADisplayLink   *_gameTimer;

}

@end

@implementation MainViewController

/*

 使用DEPRECATED描述符的方法和对象,是不推荐使用的,同时也是官方停止更新的方法

 但是,只要存在,就能使用!

 提示:加速剂默认是不工作,因为工作会耗电,当设置了采样频率,加速剂开始工作,同时将采样获得的数据

 通过代理方法,发送给调用方

  UIAcceleration的说明

 * timestamp    数据采样发生的时间

 * x            x 方向的加速度

 * y            y 方向的加速度

 * z            z 方向的加速度

 }

 */

- (void)viewDidLoad

{

    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"black.png"];

    _ball = [[UIImageView alloc]initWithImage:image];

    [_ball setCenter:self.view.center];

    [self.view addSubview:_ball];

    

    // 小球初始静止

    _ballVelocity = CGPointZero;

    // 加速计

    // 1. 实例化加速计,因为在手机上有且仅有一个芯片,因此使用单例来访问加速计

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

    // 2. 设置更新频率(采样频率)

    [accelerometer setUpdateInterval:1 / 30.0];

    // 3. 设置代理

    [accelerometer setDelegate:self];

    // 游戏时钟

    // 1. 实例化

    _gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];

    // 2. 主运行循环

    [_gameTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

}

#pragma mark - 时钟监听方法

- (void)step

{

    [self updateBallLocation];

}

#pragma mark - 更新小球位置

- (void)updateBallLocation

{

    // 根据小球位置调整中心点位置

    CGPoint center = _ball.center;

    

    // 判断小球的位置是否超出边界,如果超出边界,将小球的方向求反

    // 1) 水平方向

    // 如果小球的最小x值,小于0,表示左边出界

    // CGRectGetMinX(_ball.frame) = _ball.frame.origin.y

    // 如果小球的最大x值,大于viewW,表示右边边出界

    if (CGRectGetMinX(_ball.frame) < 0 || CGRectGetMaxX(_ball.frame) > self.view.bounds.size.width) {

        _ballVelocity.x *= -0.8;   

        // 修复小球位置

        if (CGRectGetMinX(_ball.frame) < 0) {

            center.x = _ball.bounds.size.width / 2;

        } else {

            center.x = self.view.bounds.size.width - _ball.bounds.size.width / 2;

        }

    }   

    // 2)垂直方向

    if (CGRectGetMinY(_ball.frame) < 0 || CGRectGetMaxY(_ball.frame) > self.view.bounds.size.height) {

        _ballVelocity.y *= -0.8;

        // 修复小球位置

        if (CGRectGetMinY(_ball.frame) < 0) {

            center.y = _ball.bounds.size.height / 2;

        } else {

            center.y = self.view.bounds.size.height - _ball.bounds.size.height / 2;

        }

    }

    

    center.x += _ballVelocity.x;

    center.y += _ballVelocity.y;

    

    [_ball setCenter:center];

}

#pragma mark - 加速计代理方法

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

{

    // 使用加速度调整小球速度

    _ballVelocity.x += acceleration.x;

    _ballVelocity.y -= acceleration.y;

    

    // 让加速剂仅负责采样数据,更新速度

    [self updateBallLocation];

}

@end

原文地址:https://www.cnblogs.com/sixindev/p/4522941.html