环球电气之家-午夜精彩视频-中国专业电气电子产品行业服务网站!

產品分類

當前位置: 首頁 > 傳感測量產品 > 工業傳感器 > 力傳感器

類型分類:
科普知識
數據分類:
力傳感器

ios 重力傳感器:iOS 重力感應之箭頭指向重力方向

發布日期:2022-10-09 點擊率:32


ios 重力傳感器:iOS 重力感應之箭頭指向重力方向  第1張

ios 重力傳感器:iOS 重力感應之箭頭指向重力方向

以屏幕的左下方為原點(2d編程的時候,是以屏幕左上方為原點的,這個值得注意一下),箭頭指向的方向為正.從-10到10,以浮點數為等級單位,想象一下以下情形:手機屏幕向上(z軸朝天)水平放置的時侯,(x,y,z)的值分別為(0,0,10);手機屏幕向下(z軸朝地)水平放置的時侯,(x,y,z)的值分別為(0,0,-10);手機屏幕向左側放(x軸朝天)的時候,(x,y,z)的值分別為(10,0,0);手機豎直(y軸朝天)向上的時候,(x,y,z)的值分別為(0,10,0);其他的如此類推,規律就是:朝天的就是正ios 重力傳感器:iOS 重力感應之箭頭指向重力方向  第2張

ios 重力傳感器:IOS學習筆記-加速度傳感器(重力感應)-UIAccelerometer

@interface?DSViewController :?UIViewController?

{

//我們用一個label來表示隨加速度方向運動的小方塊

UILabel?*_label;

//x軸方向的速度

UIAccelerationValue?_speedX;

//y軸方向的速度

UIAccelerationValue?_speedY;

}

@end

?

@implementation?DSViewController

- (void)viewDidLoad

{

[super?viewDidLoad];

?

self.view.backgroundColor?= [UIColor?yellowColor];

CGRect?winRect = [UIScreen?mainScreen].applicationframe;

//實例化 隨加速度方向運動的小方塊(label)

_label?= [[UILabel?alloc]initWithframe:CGRectMake(0,?0,?80,?80)];

_label.center?=?CGPointMake(winRect.size.width?*?0.5, winRect.size.height?*?0.5);

_label.text?=?@"Droid";

_label.textAlignment?=?UITextAlignmentCenter;

_label.backgroundColor?= [UIColor?greenColor];

[self.view addSubview:_label];

[_label release];

}

-(void)viewWillAppear:(BOOL)animated

{

[super?viewWillAppear:animated];

//召喚加速度傳感器

UIAccelerometer?*accelerometer = [UIAccelerometer?sharedAccelerometer];

//設置加速度傳感器的 接收加速度通知的時間間隔

//設置為1.0/60.0表示一秒接收60次,可根據實際需求調整

accelerometer.updateInterval?=?1.0/60.0;

//下面這個不設置,代理方法就不會調用

accelerometer.delegate?=?self;

}

-(void)viewWillDisappear:(BOOL)animated

{

[super?viewWillDisappear:animated];

//不要忘了停止傳感器的工作

//結束加速度傳感器的工作

_speedX?=?_speedY?=?0;

UIAccelerometer?*accelerometer = [UIAccelerometer?sharedAccelerometer];

accelerometer.delegate?=?nil;

}

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

{

//獲得的加速度要考慮到加速度傳感器的原點是物理重心,而不是屏幕右上角

//x軸方向的速度加上x軸方向獲得的加速度

_speedX?+= acceleration.x;

//y軸方向的速度加上y軸方向獲得的加速度

_speedY?+= acceleration.y;

//小方塊將要移動到的x軸坐標

CGFloat?posX =?_label.center.x?+?_speedX;

//小方塊將要移動到的y軸坐標

CGFloat?posY =?_label.center.y?-?_speedY;

//碰到屏幕邊緣反彈

if?(posX ?self.view.bounds.size.Width</span>){

posX =?self.view.bounds.size.Width</span>;

//碰到屏幕右邊以0.4倍的速度反彈

_speedX?*= -0.4;

}

if?(posY ?self.view.bounds.size.Height</span>){

posY =?self.view.bounds.size.Height</span>;

//碰到屏幕下邊以1.5倍的速度反彈

_speedY?*= -1.5;

}

//移動小方塊

_label.center?=?CGPointMake(posX, posY);

}

@end

首尾呼應:加速度傳感器使用很easy有木有!

ios 重力傳感器:IOS的重力感應

IOS的重力感應

昨天寫了重力感應的例子,我覺得這個例子比較有用處,我分享出來:
1 )顯然ios4 之后可以使用coreMotion的framework 為了向下兼容加上UIAccelerator,
[html]
#import

@end
CMMotionManager 將是我們使用的Object,可以用來監測重力!
同時,咱們不能在需要監測重力感應的地方直接使用這個類,這樣耦合比較嚴重,也不利于重用。所以抽離出來,在代碼中您可以看到,我將定義一個signleton,同時將重力變化的事件回調給其代理。
2.接著往下是定義其函數,這個很簡單,直接貼代碼。
[html]
#import "IFAccelerometer.h"
static IFAccelerometer *accelerometerInstance=nil;
@implementation IFAccelerometer
+ (id)shareAccelerometer
{
if (!accelerometerInstance) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
accelerometerInstance=[[[self class]alloc]init];
});
}
return accelerometerInstance;
}

- (id)init
{
self=[super init];
if (self) {
#ifdef __IPHONE_5_0
_motionManager=[[CMMotionManager alloc]init];
if (_motionManager.accelerometerAvailable) {
[_motionManager setAccelerometerUpdateInterval:1/60.f];
NSOperationQueue *operationQueue=[NSOperationQueue mainQueue];
[_motionManager startAccelerometerUpdatesToQueue:operationQueue withHandler:^(CMAccelerometerData *data,NSError *error)
{
if ([_delegate respondsToSelector:@selector(accelerateWithX:withY:withZ:withTimeInterval:)])
{
NSNumber *x =[NSNumber numberWithDouble:data.acceleration.x];
NSNumber *y =[NSNumber numberWithDouble:data.acceleration.y];
NSNumber *z =[NSNumber numberWithDouble:data.acceleration.z];
[_delegate accelerateWithX:x withY:y withZ:z withTimeInterval:data.timestamp];
}
}
];

}
#else
#ifdef __IPHONE_4_0
  _accelerometer=[UIAccelerometer sharedAccelerometer];
  [_accelerometer setUpdateInterval:(1/60.0f)];
  _accelerometer.delegate=self;
#endif
#endif

}
return self;
}

- (void)addOberser:(id)oberserer
{
_delegate=oberserer;
}

- (void)removeObserver
{
_delegate=nil;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if ([_delegate respondsToSelector:@selector(accelerateWithX:withY:withZ:withTimeInterval:)])
{
NSNumber *x =[NSNumber numberWithDouble:acceleration.x];
NSNumber *y =[NSNumber numberWithDouble:acceleration.y];
NSNumber *z =[NSNumber numberWithDouble:acceleration.z];
[_delegate accelerateWithX:x withY:y withZ:z withTimeInterval:acceleration.timestamp];
}

}
3.以ViewController 為例介紹如何使用重力感應
[html]
- (void)viewDidLoad
{
[super viewDidLoad];
[[IFAccelerometer shareAccelerometer]addOberser:self];
_imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"33.png"]];
_imageView.frame=CGRectMake(0, 0, KIMAGEWIDTH, KIMAGEHEIGHT);
_imageView.center=self.view.center;
[self.view addSubview:_imageView];

}
_imageView 是一個UIImageView的成員,其寬高是一個分別是我定義的宏。
注意,我在頭文件添加了重力感應的代理,此時這行
[[IFAccelerometer shareAccelerometer]addOberser:self];
表明我在這個viewController中使用這個重力感應的數據。
好,現在是完成回調的時候了,繼續貼代碼
[html]
- (void)accelerateWithX:(NSNumber *)x withY:(NSNumber *)y withZ:(NSNumber *)z withTimeInterval:(NSTimeInterval)timeInterval
{

float deceleration=0.4f;
float sensitivity =6.0f;
float maxVelocity =100.0f;

velocity.x=velocity.x * deceleration + [x doublevalue] * sensitivity;
velocity.y=velocity.y * deceleration + [y doublevalue] * sensitivity;

if(velocity.x > maxVelocity){
velocity.x=maxVelocity;
}else if(velocity.x < -maxVelocity){        velocity.x=-maxVelocity;    }        if(velocity.y > maxVelocity){
velocity.y=maxVelocity;
}else if(velocity.y < -maxVelocity){        velocity.y=-maxVelocity;    }        CGPoint pos=_imageView.center;    pos.x +=velocity.x;    pos.y -=velocity.y;        float imageWidthHalved=  KIMAGEWIDTH   * 0.5f;    float leftBorderLimit =0.0f;    float rightBorderLimit=0.0f;    if (imageWidthHalved>self.view.frame.size.width/2.0f) {
leftBorderLimit =   self.view.frame.size.width - imageWidthHalved;
rightBorderLimit=  imageWidthHalved;
}
else
{
leftBorderLimit =   imageWidthHalved ;
rightBorderLimit=  self.view.frame.size.width - imageWidthHalved;
}

float imageHeightHalved=KIMAGEHEIGHT * 0.5f;
float topBorderLimit   =0.0f;
float bottomBorderLimit=0.0f;
if (imageHeightHalved>self.view.frame.size.height/2.0f) {
topBorderLimit   =self.view.frame.size.height - imageHeightHalved;
bottomBorderLimit=  imageHeightHalved ;
}
else
{
topBorderLimit   =imageHeightHalved ;
bottomBorderLimit=self.view.frame.size.height - imageHeightHalved  ;
}

if(pos.x < leftBorderLimit){        pos.x=leftBorderLimit;        velocity=CGPointZero;    }else if(pos.x > rightBorderLimit){
pos.x=rightBorderLimit;
velocity=CGPointZero;
}

if(pos.y < topBorderLimit){        pos.y=topBorderLimit;        velocity=CGPointZero;    }else if(pos.y > bottomBorderLimit){
pos.y=bottomBorderLimit;
velocity=CGPointZero;
}

_imageView.center=pos;
}
上面是對于邊界的處理等等操作,都很簡單,不一一介紹了。

相關文章暫無相關文章
ios 重力傳感器:iOS 重力感應之箭頭指向重力方向  第3張

ios 重力傳感器:IOS重力感應

iPhone和iPad設備有4個方向的狀態,我們可以針對應用當前所處的方向調整界面。
為了使應用支持不同的方向,首先我們需要在項目設置中設置設備支持的方向(也可以在項目的plist中設置)
Portrait  豎放,home鍵在屏幕下方
Upside Down  豎放,home鍵在屏幕上方
Landscape Left  橫放,home鍵在屏幕左方
Landscape Right  橫放,home鍵在屏幕右方

我們在StoryBoard中拖入一個新的ViewController,綁定好它的File's Owner,然后放入6個UIView,設置好不同的背景色

程序運行后,我們在旋轉模擬器,發現在豎屏和橫屏狀態下,界面顯示如下
    

顯然橫屏狀態下這樣的顯示是不適合的,為了得到合適的顯示效果,我們需要在ViewController中寫一些代碼。
首先我們先看一下在ViewController中和重力感應相關的一些函數
- (BOOL)shouldAutorotate  
此函數返回YES表示此視圖控制器支持重力感應,返回NO表示此視圖控制器不支持重力感應

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation  
此函數設置視圖控制器加載后最先顯示的方向,UIInterfaceOrientation是一個結構體,支持的取值如下
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight

- (NSUInteger)supportedInterfaceOrientations
此函數設置視圖控制器支持的方向(需要shouldAutorotate返回YES),支持的取值如下
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll=(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown)
UIInterfaceOrientationMaskAllButUpsideDown

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
用戶界面即將旋轉時觸發此函數,toInterfaceOrientation表示即將到達的方向

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
用戶界面旋轉結束時觸發此函數,fromInterfaceOrientation表示旋轉前的方向

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
用戶界面旋轉過程中觸發此函數,一般在此函數中定制翻轉后控件的位置和大小

所以在上面那個實例中我們先將6個UIVIew控件連結到視圖控制器中,然后在willAnimateRotationToInterfaceOrientation:duration:中修改旋轉后的界面

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.view1.frame =CGRectMake(50, 20, 110, 130);
self.view2.frame =CGRectMake(225, 20, 110, 130);
self.view3.frame =CGRectMake(400, 20, 110, 130);

self.view4.frame =CGRectMake(50, 180, 110, 130);
self.view5.frame =CGRectMake(225, 180, 110, 130);
self.view6.frame =CGRectMake(400, 180, 110, 130);
}
}

上面代碼在旋轉到橫屏時修改view控件的位置,因為我們strobyboard中默認布局了豎直狀態下的界面,所以在代碼中不需要重新布局view控件豎直狀態時的位置,但是如果我們是用編碼方式放置的控件,那么需要在上面代碼中添加一個else,設置豎屏后的界面。
    

下一篇: PLC、DCS、FCS三大控

上一篇: 電氣控制線路圖控制原

主站蜘蛛池模板: 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 防爆正压柜厂家_防爆配电箱_防爆控制箱_防爆空调_-盛通防爆 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 慢回弹测试仪-落球回弹测试仪-北京冠测精电仪器设备有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 房车价格_依维柯/大通/东风御风/福特全顺/江铃图片_云梯搬家车厂家-程力专用汽车股份有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 高压绝缘垫-红色配电房绝缘垫-绿色高压绝缘地毯-上海苏海电气 | 维泰克Veertek-锂电池微短路检测_锂电池腐蚀检测_锂电池漏液检测 | 气胀轴|气涨轴|安全夹头|安全卡盘|伺服纠偏系统厂家-天机传动 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 扒渣机厂家_扒渣机价格_矿用扒渣机_铣挖机_撬毛台车_襄阳永力通扒渣机公司 | 吹田功率计-长创耐压测试仪-深圳市新朗普电子科技有限公司 | 出国劳务公司_正规派遣公司[严海] | 石栏杆_青石栏杆_汉白玉栏杆_花岗岩栏杆 - 【石雕之乡】点石石雕石材厂 | 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 升降炉_真空气氛炉_管式电阻炉厂家-山东中辰电炉有限公司 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 苏州伊诺尔拆除公司_专业酒店厂房拆除_商场学校拆除_办公楼房屋拆除_家工装拆除拆旧 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | 无锡网站建设_企业网站定制-网站制作公司-阿凡达网络 | 小小作文网_中小学优秀作文范文大全| CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 深圳市东信高科自动化设备有限公司 | 氧氮氢联合测定仪-联测仪-氧氮氢元素分析仪-江苏品彦光电 | 国产频谱分析仪-国产网络分析仪-上海坚融实业有限公司 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 早报网 | 首页 - 军军小站|张军博客 |