加速传感器

P:由于硬件相关的都封装好了,比较简单,这里直接介绍方法

步骤:

1、获得单例对象:UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

   2、设置代理:accelerometer.delegate = self;

  3、设置采样间隔:accelerometer.updateInterval = 1.0/30.0; // 1秒钟采样30次

   4、实现代理方法:- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

// acceleration中的x、y、z三个属性分别代表每个轴上的加速度

代码:

#import "ViewController.h"
#import "UIView+AdjustFrame.h" // 一个分类,用来直接取width,height等

@interface ViewController () <UIAccelerometerDelegate>

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

// 保留x,y轴上面的速度
@property(nonatomic,assign)CGPoint velocity;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.获取单例对象
    UIAccelerometer *acclerometer = [UIAccelerometer sharedAccelerometer];

    // 2.设置代理
    acclerometer.delegate = self;

    // 3.设置采样间隔
    acclerometer.updateInterval = 1 / 30.0;
}

/**
 *  获取到加速计信息的时候会调用该方法
 *
 *  @param acceleration  里面有x,y,z抽上面的加速度
 */
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    // NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    // 1.计算小球速度(对象的结构内部的属性是不能直接赋值)
    _velocity.x += acceleration.x;
    _velocity.y += acceleration.y;

    // 2.让小球移动
    /*
    CGRect ballFrame = self.ball.frame;
    ballFrame.origin.x += _velocity.x;
    self.ball.frame = ballFrame;
     */
    self.ball.x += _velocity.x;
    self.ball.y -= _velocity.y;

    // 3.边界判断
    if (self.ball.x <= 0) {
        self.ball.x = 0;
        _velocity.x *= -0.6;
    }
    if (self.ball.x >= self.view.width - self.ball.width) {
        self.ball.x = self.view.width - self.ball.width;
        _velocity.x *= -0.6;
    }
    if (self.ball.y <= 0) {
        self.ball.y = 0;
        _velocity.y *= -0.6;
    }
    if (self.ball.y >= self.view.height - self.ball.height) {
        self.ball.y = self.view.height - self.ball.height;
        _velocity.y *= -0.6;
    }

    NSLog(@"x:%f y:%f", _velocity.x, _velocity.y);
}

由于是真机调试,没有截图,效果是跳来跳去

时间: 2024-08-25 18:58:46

加速传感器的相关文章

Android 获取加速传感器的值,并去除杂音

1.注册和注销传感器 private void registerSensor() { manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorMan

windows phone传感器

Windows phone中的传感器主要包括加速计传感器.罗盘传感器.陀螺仪传感器等 加速计传感器 Accelerometer类是加速传感器的接口,Accelerometer类位于Windows.Devices.Sensors命名空间下. 要使用系统加速计的功能,需要创建一个Accelerometer类的对象,然后用这个对象来捕获手机当前的加速状态. Accelerometer类提供了ReadingChanged事件用于检测加速计的状态,并返回X.Y.Z轴信息. 使用Accelerometer类

ASF(传感器)

版权声明:以前的Blog文章合并.原创作品,谢绝转载!否则将追究法律责任. SensorManager类:用于创建sensor service的实例.该类提供了很多 用于访问和枚举传感器,注册和注销传感器监听器的方法.而且还提供了 与传感器精度.扫描频率.校正有关的常量. Sensor类:提供了一些用于获取传感器技术参数的方法.如版本.类型. 生产商等. 1. TYPE_ACCELEROMETER:加速传感器(硬件传感器) 2. TYPE_AMBIENT_TEMPERATURE:温度传感器(硬件

Android 使用加速度传感器实现摇一摇功能及优化

如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得传感器管理器的实例 sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 2.通过传感器管理器获得加速传感器 accelerateSensor = getSensorManager(cont

Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)

主要介绍一下android的加速传感器的简单用法,模拟摇一摇 ,如果x,y,z三个方向的加速度超过了15,就会弹出Toast,当然你可以设置更复杂的策略,比如判断间隔 代码如下 public class MainActivity extends Activity { private SensorManager sensorManager; private TextView textView; @Override protected void onCreate(Bundle savedInstan

Android传感器Check小Demo

昨晚写了个关于Sensor的很简单的Demo,就是Check一下手机或者平板有没有所检测的传感器.由于今天一直在忙,现在才总结一下. 先看下运行截图: 三星Note 3: 三星GT-N8000: 下面是代码: package com.example.sensorcheck; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hard

Android中传感器Sensor的使用

Android中传感器Senso的使用 1.Sensor类型 Android中有多种传感器,目前Android SDK支持的传感器有:光线传感器,旋转向量传感器,压力传感器,陀螺仪传感器,加速度传感器,重力传感器,方向传感器,磁场传感器,近程传感器等.但并不是所有手机都具有全部传感器,一般高端手机具有大多数传感器,毕竟传感器都需要money的,价格自然不菲. 2.Sensor实际应用  那么在Android开发中,如何使用传感器,将传感器功能添加到Android应用中呢,例如微信的摇一摇,通过加

iOS_传感器的使用

一.距离传感器. 大概解释一下距离传感器的功能:它会根据你和iphone间距离不同做出不同的响应. [objc] view plain copy print? //打开距离传感器 [UIDevice currentDevice].proximityMonitoringEnabled = YES; //监听距离传感器状态变化通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorState

Android 传感器开发 完全解析

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/53161452 大家好,由于最近会有对智能硬件相关的开发需求,所以最近这些天分享的博文也就大致挂钩智能硬件了,像上一篇的蓝牙分享,相信很多读者已经看过了,那么今天我为大家带来Android传感器方面知识的介绍与使用方法,对于传感器的使用,不同版本的Android手机也许存在较大的硬件差异,但是万变不离其宗,本篇将通过几个最常见的传感器,渗透式的教会大家如何使用这些传感器,带领大家完