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

產(chǎn)品分類

當前位置: 首頁 > 傳感測量產(chǎn)品 > 工業(yè)傳感器 > 力傳感器

類型分類:
科普知識
數(shù)據(jù)分類:
力傳感器

ios 重力傳感器:iOS 重力感應(yīng)之箭頭指向重力方向

發(fā)布日期:2022-10-09 點擊率:31


ios 重力傳感器:iOS 重力感應(yīng)之箭頭指向重力方向  第1張

ios 重力傳感器:iOS 重力感應(yīng)之箭頭指向重力方向

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

ios 重力傳感器:IOS學習筆記-加速度傳感器(重力感應(yīng))-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];

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

//設(shè)置為1.0/60.0表示一秒接收60次,可根據(jù)實際需求調(diào)整

accelerometer.updateInterval?=?1.0/60.0;

//下面這個不設(shè)置,代理方法就不會調(diào)用

accelerometer.delegate?=?self;

}

-(void)viewWillDisappear:(BOOL)animated

{

[super?viewWillDisappear:animated];

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

//結(jié)束加速度傳感器的工作

_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

首尾呼應(yīng):加速度傳感器使用很easy有木有!

ios 重力傳感器:IOS的重力感應(yīng)

IOS的重力感應(yīng)

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

@end
CMMotionManager 將是我們使用的Object,可以用來監(jiān)測重力!
同時,咱們不能在需要監(jiān)測重力感應(yīng)的地方直接使用這個類,這樣耦合比較嚴重,也不利于重用。所以抽離出來,在代碼中您可以看到,我將定義一個signleton,同時將重力變化的事件回調(diào)給其代理。
2.接著往下是定義其函數(shù),這個很簡單,直接貼代碼。
[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 為例介紹如何使用重力感應(yīng)
[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的成員,其寬高是一個分別是我定義的宏。
注意,我在頭文件添加了重力感應(yīng)的代理,此時這行
[[IFAccelerometer shareAccelerometer]addOberser:self];
表明我在這個viewController中使用這個重力感應(yīng)的數(shù)據(jù)。
好,現(xiàn)在是完成回調(diào)的時候了,繼續(xù)貼代碼
[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;
}
上面是對于邊界的處理等等操作,都很簡單,不一一介紹了。

相關(guān)文章暫無相關(guān)文章
ios 重力傳感器:iOS 重力感應(yīng)之箭頭指向重力方向  第3張

ios 重力傳感器:IOS重力感應(yīng)

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

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

程序運行后,我們在旋轉(zhuǎn)模擬器,發(fā)現(xiàn)在豎屏和橫屏狀態(tài)下,界面顯示如下
    

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

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

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

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
用戶界面旋轉(zhuǎn)結(jié)束時觸發(fā)此函數(shù),fromInterfaceOrientation表示旋轉(zhuǎn)前的方向

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

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

- (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);
}
}

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

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

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

主站蜘蛛池模板: 液压升降货梯_导轨式升降货梯厂家_升降货梯厂家-河南东圣升降设备有限公司 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 上海橡胶接头_弹簧减震器_金属软接头厂家-上海淞江集团 | 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 气动调节阀,电动调节阀,自力式压力调节阀,切断阀「厂家」-浙江利沃夫自控阀门 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 胀套-锁紧盘-风电锁紧盘-蛇形联轴器「厂家」-瑞安市宝德隆机械配件有限公司 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 大白菜官网,大白菜winpe,大白菜U盘装系统, u盘启动盘制作工具 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 罐体电伴热工程-消防管道电伴热带厂家-山东沃安电气 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | 法兰螺母 - 不锈钢螺母制造厂家 - 万千紧固件--螺母街 | 广东青藤环境科技有限公司-水质检测 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 防水套管|柔性防水套管|伸缩器|伸缩接头|传力接头-河南伟创管道 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | led太阳能路灯厂家价格_风光互补庭院灯_农村市政工程路灯-中山华可路灯品牌 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 天空彩票天下彩,天空彩天空彩票免费资料,天空彩票与你同行开奖,天下彩正版资料大全 | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 航拍_专业的无人机航拍摄影门户社区网站_航拍网 | 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 事迹材料_个人事迹名人励志故事 学生作文网_中小学生作文大全与写作指导 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 深圳昂为官网-气体分析仪,沼气分析仪,动态配气仪,气体传感器厂家 | 标准品网_标准品信息网_【中检计量】 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 西子馋火锅鸡加盟-太原市龙城酉鼎餐饮管理有限公司 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 |