欧美成人黄色网_欧美精品久久_国产在线一区二区三区_免费视频久久久_亚洲二区视频_欧美大片免费高清观看

產(chǎn)品分類

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

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

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

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


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

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

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

ios 重力傳感器:IOS學(xué)習(xí)筆記-加速度傳感器(重力感應(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軸坐標(biāo)

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

//小方塊將要移動到的y軸坐標(biāo)

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)的地方直接使用這個類,這樣耦合比較嚴(yán)重,也不利于重用。所以抽離出來,在代碼中您可以看到,我將定義一個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)用當(dā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表示即將到達(dá)的方向

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

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

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

主站蜘蛛池模板: 亚洲高清在线播放 | 在线观看国产三级 | 久草综合在线视频 | 亚洲精品乱码久久久久久按摩 | 亚洲一区二区中文字5566 | 欧美成人久久电影香蕉 | 亚洲视频中文字幕在线观看 | 妞干在线 | 少数民族美乳国产在线 | 看全色黄大色黄大片色黄看的 | 国产aⅴ无码专区亚洲av | 久久婷婷五月综合色国产 | 免费在线一级片 | 成人午夜精品久久不卡 | 精品国产一区二区三区麻豆小说 | 国产成人高清亚洲一区91 | 无码人妻一区二区三区在线视频 | 亚洲一区二区三区精品视频 | 优优人体大尺大尺无毒不卡 | 男女一级毛片免费视频看 | 这里只有精品久久 | 久久久久亚洲精品成人网小说 | 欧美激情精品久久久久久 | 精品少妇爆乳无码av无码专区 | 亚洲精品国产综合99久久一区 | 老司机亚洲精品 | 国产乱码精品一区二区三 | 国产黄a三级三级三级 | 日本最新免费二区三区 | 丰满少妇69激情啪啪无 | 精品国产免费一区二区 | 鲁一鲁一鲁一鲁一曰综合网 | 精品国产综合区久久久久99 | 国产av一区二区三区最新精品 | 欧美性xxxx极品hd欧美风情 | 琪琪色原网站在线观看 | 99精品久久久久久久免费看蜜月 | 色窝窝免费一区二区三区 | 午夜网页 | 熟女乱牛牛视频在线观看 | 在线国产一区 |