一 概述

加速计的作用 :用于 检测设备的运动(比如摇晃)。本文介绍相关的两个示例

  • 控制小球的移动
  • 摇一摇

二 控制小球的移动

2.1 项目描述

  • Storyboard上事先放置一个Ball
  • 随着手机的移动,小球随着上下左右移动
  • 超出边界检测(上下左右边界 ),放置到上下左右边界处

2.2 代码

项目代码

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
#import "UIView+Extension.h"@interface ViewController ()
//小球
@property (strong, nonatomic) IBOutlet UIView *imageBall;
//保存速度
@property(nonatomic,assign) CGPoint velocity;
@property(nonatomic,strong) CMMotionManager *mgr;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self push];
}//-push方式采集
-(void)push
{//1-创建CoreMotion管理者//CMMotionManager *mgr=[[CMMotionManager alloc]init];self.mgr=[[CMMotionManager alloc]init];//2-判断加速计 是否可用if (self.mgr.isAccelerometerActive) {//3-设置采样时间self.mgr.accelerometerUpdateInterval=1/30.0;//4-开始采样[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {//这个block是采集到数据时就会调用if (error) return;CMAcceleration acceleration=accelerometerData.acceleration;NSLog(@"x=%f,y=%f,z=%f",acceleration.x,acceleration.y,acceleration.z);//移动速度_velocity.x+=acceleration.x;_velocity.y-=acceleration.y;//移动距离self.imageBall.x+=_velocity.x;self.imageBall.y+=_velocity.y;//边界检测if (self.imageBall.x<=0) {//矫正小球当前的位置self.imageBall.x=0;//超出了屏幕的左边_velocity.x*=-0.5;}if (self.imageBall.y<=0) {//矫正小球当前的位置self.imageBall.y=0;//超出屏幕的顶部_velocity.y*=-0.5;}if (CGRectGetMaxY(self.imageBall.frame)>=self.view.height) {//矫正小球当前的位置self.imageBall.y=self.view.height-self.imageBall.height;//超出屏幕的底部_velocity.y*=-0.5;}if (CGRectGetMaxX(self.imageBall.frame)>=self.view.width) {//矫正小球当前的位置self.imageBall.x=self.view.width-self.imageBall.width;//超出屏幕的右边_velocity.x*=-0.5;}}];}else{NSLog(@"加速计不可用");}
}
@end

UIView+Extension.h

#import <UIKit/UIKit.h>@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat maxX;
@property (nonatomic, assign) CGFloat maxY;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@end

UIView+Extension.m

#import "UIView+Extension.h"@implementation UIView (Extension)- (void)setX:(CGFloat)x
{CGRect frame = self.frame;frame.origin.x = x;self.frame = frame;
}- (CGFloat)x
{return self.frame.origin.x;
}- (void)setMaxX:(CGFloat)maxX
{self.x = maxX - self.width;
}- (CGFloat)maxX
{return CGRectGetMaxX(self.frame);
}- (void)setMaxY:(CGFloat)maxY
{self.y = maxY - self.height;
}- (CGFloat)maxY
{return CGRectGetMaxY(self.frame);
}- (void)setY:(CGFloat)y
{CGRect frame = self.frame;frame.origin.y = y;self.frame = frame;
}- (CGFloat)y
{return self.frame.origin.y;
}- (void)setCenterX:(CGFloat)centerX
{CGPoint center = self.center;center.x = centerX;self.center = center;
}- (CGFloat)centerX
{return self.center.x;
}- (void)setCenterY:(CGFloat)centerY
{CGPoint center = self.center;center.y = centerY;self.center = center;
}- (CGFloat)centerY
{return self.center.y;
}- (void)setWidth:(CGFloat)width
{CGRect frame = self.frame;frame.size.width = width;self.frame = frame;
}- (CGFloat)width
{return self.frame.size.width;
}- (void)setHeight:(CGFloat)height
{CGRect frame = self.frame;frame.size.height = height;self.frame = frame;
}- (CGFloat)height
{return self.frame.size.height;
}- (void)setSize:(CGSize)size
{
//    self.width = size.width;
//    self.height = size.height;CGRect frame = self.frame;frame.size = size;self.frame = frame;
}- (CGSize)size
{return self.frame.size;
}@end

2.3 预览

暂无设备,自行测试

三摇一摇

3.1 说明

  • 在AppDelegate中重写:motionBegan(摇晃开始)、motionEnded(摇晃结束)、motionCancelled(摇晃取消)相关方法
  • 安装到手机中,晃动手机,查看方法的执行结果

3.2 代码

#import "AppDelegate.h"@interface AppDelegate ()
@end
@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.return YES;
}#pragma mark -摇一摇
//摇一摇开始
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"%s",__func__);}
//摇一摇结束
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"%s",__func__);
}
//摇一摇取消
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{NSLog(@"%s",__func__);
}
@end

IOS开发之——硬件开发-加速计应用实例(04)相关推荐

  1. 软件开发、硬件开发、IPD产品开发 及 工程开发各阶段划分

    软件开发.硬件开发.IPD产品开发 及 工程开发各阶段划分 参考链接:https://zhuanlan.zhihu.com/p/427246890 1.软件开发阶段划分: Alpha.Beta.RC. ...

  2. 软件开发和硬件开发编程的比较

    [对本站的Web文本编辑器感到头疼,我在记事本里编的代码整整齐齐,贴到这里就歪歪扭扭.] /// <summary>/// C# .Net满意度的枚举./// </summary&g ...

  3. IOS开发之——硬件开发-蓝牙(06)

    一 概述 本文介绍通过CoreBluetooth进行蓝牙操作的示例,包含 项目界面介绍 蓝牙权限授予 功能开发(蓝牙扫描.停止扫描.清空设备) 二 项目界面介绍 说明: 上面是个UITableView ...

  4. IOS开发之——硬件开发-蓝牙(07)

    一 概述 点击设备列表连接设备 设备连接情况说明 设备连接成功后扫描Service服务 Service服务获取之后获取CBCharacteristic特征值 二 点击设备列表连接设备 2.1 设置UI ...

  5. adrv9003/ADRV9001/ADRV9002 FPGA驱动开发(硬件开发)

    前面有简单介绍下ADRV9002,具体可参见 https://blog.csdn.net/jingjiankai5228/article/details/124436709 9001系列芯片相比之前的 ...

  6. 【连载】【FPGA黑金开发板】NIOS II那些事儿--硬件开发(一)

     声明:本文为原创作品,版权归黑金动力社区(http://www.heijin.org)所有,如需转载,请注明出处http://www.cnblogs.com/kingst/ 前言 从今天开始,NIO ...

  7. iOS蓝牙原生封装,助力智能硬件开发

    代码地址如下: http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手 ...

  8. 【STM32-V5】STM32F407开发板开源, 丰富软件资源, 强劲硬件配置, 配套600实例, 20套手册带视频教程2023-05-15

    从2013年5月份发布至今,开发板硬件更新过6个版本,软件资料更新过136次.当前标准库最新版本V9.2,HAL库最新版本V5.6 安富莱微信公共平台,欢迎大家关注(打造高质量公众号). ====== ...

  9. IOS App的简单开发实例

    在互联网浪潮下,APP的开发逐渐变得潮流和简易.而当下开发APP也成了热门行业,在现代丰富的网络资源和强大工具,环境的支持下,这一项技能也逐渐被越来越多的人掌握.但是一个APP从构想到上市,需要历经很 ...

  10. iOS cocos2d 2游戏开发实战(第3版)---你的第一个游戏!

    2019独角兽企业重金招聘Python工程师标准>>> 随着苹果公司不断地创新与发展,新的iPhone 5.iPad 4以及iPad mini产品相继问世,包括iOS与Xcode在内 ...

最新文章

  1. tplink wr886n v5.0 ttl 接线方法
  2. neat算法——本质就是遗传算法用于神经网络的自动构建
  3. tta部署_YOLOv5项目介绍
  4. JVM学习笔记(三)------内存管理和垃圾回收
  5. DL之VGG16:基于VGG16(Keras)利用Knifey-Spoony数据集对网络架构进行迁移学习
  6. 7-35 城市间紧急救援 (25 分)(思路加详解)
  7. Spark源码走读10——Spark On Yarn
  8. ANTLR-语法树遍历机制
  9. UVa 202 Repeating Decimals
  10. 445.两数相加II
  11. 预测未来的神技---马尔科夫模型
  12. LeetCode93—Restore IP Addresses
  13. C++ API设计笔记
  14. Cisco:CCNA专业英文词汇(1)
  15. 米的换算单位和公式_Excel 怎样进行单位换算,excle怎么把米换算公里
  16. Linux僵尸进程分析清除
  17. 想要做好自媒体?大周给你分享几点心得
  18. win10内置Ubuntu安装图形界面
  19. 服务器能用usb pe安装win7系统,巧用U盘在win8PE下安装win7系统的教程
  20. 【百度】 快速精准搜索

热门文章

  1. Fiddler https最新抓包方法(Android 9.0)
  2. Android模拟器 使用 Fiddler抓包
  3. adb下载安装教程(已安装Android studio)
  4. 码栈——让一切变得自动化
  5. 用 MQL5 连接 EA 交易程序和 ICQ
  6. 勒索病毒的克星来了 360安全团队独家发布文件恢复工具
  7. Project(9)——收货地址 -查看列表
  8. 在vc++如何响应键盘和鼠标事件-visual c++
  9. 高通平台开发系列讲解(外设篇)BMI160基本配置
  10. SQL数据库置疑恢复