<rt id="m4md3"></rt>
  • <bdo id="m4md3"><meter id="m4md3"></meter></bdo>
  • <label id="m4md3"></label>
      <center id="m4md3"><optgroup id="m4md3"></optgroup></center>
      產品分類

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

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

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

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


      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三大控

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

      主站蜘蛛池模板: 久久综合狠狠综合久久| 久久九色综合九色99伊人| 久久综合亚洲色HEZYO国产| 久久综合狠狠综合久久97色| 狠狠色婷婷狠狠狠亚洲综合| 久久91精品久久91综合| 亚洲小说图区综合在线| 伊人色综合久久天天人守人婷 | 一日本道伊人久久综合影| 亚洲精品综合久久中文字幕| 天天综合日日噜噜噜| 亚洲一本综合久久| 色狠狠成人综合色| 亚洲av一综合av一区| 色婷婷色综合激情国产日韩| 久久综合香蕉国产蜜臀AV| segui久久综合精品| 亚洲国产综合精品| 久久综合九色综合97免费下载 | 激情综合色五月六月婷婷| 狠狠色噜狠狠狠狠色综合久| 在线亚洲97se亚洲综合在线 | 亚洲国产综合精品中文第一区| 国产综合激情在线亚洲第一页| 久久精品综合电影| 99精品国产综合久久久久五月天 | 伊人久久综合精品无码AV专区| 亚洲国产精品成人综合色在线| 色欲综合一区二区三区| 狠狠色婷婷狠狠狠亚洲综合| 国产成人亚洲综合无码| 狠狠88综合久久久久综合网| 亚洲综合在线一区二区三区| 久久综合九色综合97伊人麻豆 | 亚洲精品第一国产综合精品| 色欲人妻综合AAAAA网| 国内精品综合久久久40p| 综合激情五月综合激情五月激情1| 综合久久一区二区三区 | 狠狠色伊人久久精品综合网| 久久婷婷五月综合色国产香蕉|