加速计案例(小球移动) - (Obj-C)

实现方式:
使用加速计反应小球的受力情况,通过改变小球的frame,改变小球的位置

需要实时获取数据,所以要使用Push方式

这里只是根据加速度模拟小球移动:移动距离和加速度正相关

1.需要移动距离进行累加来表示移动距离的变化 受力移动时间越长,速度越大,移动距离越来越远

2.避免小球移出界面,判断小球的坐标,当X轴和Y轴坐标分表小于等于零和大于等于屏幕宽度/高度-自身宽度/高度时,先让小球的坐标值等于临界值,然后改变加速度方向

3.同时为了模拟移动时的阻力,系数乘以了0.8

示例代码:


#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()
// 行为管理者
@property (nonatomic,strong) CMMotionManager *manager;
// 小球
@property (nonatomic,strong) UIImageView *ballImageView;
// 每次X轴移动的距离
@property (nonatomic,assign) CGFloat tmpAccelerationX;
// 每次Y轴移动的距离
@property (nonatomic,assign) CGFloat tmpAccelerationY;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建小球
    self.ballImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball"]];
    self.ballImageView.bounds = CGRectMake(0, 0, 40, 40);
    [self.view addSubview:self.ballImageView];
    
    // 1. 创建管理者
    self.manager = [[CMMotionManager alloc]init];
    
    // 2. 设置间隔时间
    self.manager.accelerometerUpdateInterval = 0.02f;
    
    // 3. 开启检测
    [self.manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        
        CMAcceleration acceleration = accelerometerData.acceleration;
        [self ballMoveWithAccelerometerData:acceleration];
        
    }];
    
}

// 小球移动
- (void)ballMoveWithAccelerometerData:(CMAcceleration)acceleration{
    
    CGRect ballFrame = self.ballImageView.frame;
    
    self.tmpAccelerationX += acceleration.x;
    self.tmpAccelerationY -= acceleration.y;
    
    ballFrame.origin.x += self.tmpAccelerationX;
    ballFrame.origin.y += self.tmpAccelerationY;
    
    
    // 判断X轴、Y轴坐标,避免小球移出屏幕
    if (ballFrame.origin.x >= [UIScreen mainScreen].bounds.size.width - self.ballImageView.bounds.size.width) {
        
        ballFrame.origin.x = [UIScreen mainScreen].bounds.size.width - self.ballImageView.bounds.size.width;
        self.tmpAccelerationX *= -0.8;
        
    } else if (ballFrame.origin.x <= 0){
        
        ballFrame.origin.x = 0;
        self.tmpAccelerationX *= -0.8;
        
    }
    
    if (ballFrame.origin.y >= [UIScreen mainScreen].bounds.size.height - self.ballImageView.bounds.size.height){
        
        ballFrame.origin.y = [UIScreen mainScreen].bounds.size.height - self.ballImageView.bounds.size.height;
        self.tmpAccelerationY *= -0.8;
        
    }else if (ballFrame.origin.y <= 0){
        
        ballFrame.origin.y = 0;
        self.tmpAccelerationY *= -0.8;
    }
    
    self.ballImageView.frame = ballFrame;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 看了很多视频、文章,最后却通通忘记了,别人的知识依旧是别人的,自己却什么都没获得。此系列文章旨在加深自己的印象,因...
    DCbryant阅读 753评论 0 2
  • 而对于一个新人,刚刚进入某个领域,可谓摸不着头脑,不知从哪里下手! 在大学里,每到临近期末考试,同学们都会期望老师...
    宁小南阅读 535评论 0 0
  • 经常有看到一些平台分享404页面的设计,但只是丢了一些别人设计好的页面,再无其它说明。毕竟设计师都清楚设计404页...
    _Ammy阅读 2,769评论 5 39
  • 一念成佛一念成魔!悟空直到成佛又何曾真正悟到皆空!“心猿”之劫始终在于心。大圣究其平生,也无法放下“贪嗔痴”—对自...
    一蓑烟雨_886阅读 185评论 0 0
  • 1. 环境介绍 Windows10 python2.7 ipython notebook pandas 2. 问题...
    leobupt阅读 5,346评论 0 4