(iPhone/iPad開發)在iOS裝置上實現搖晃手機體驗的相關功能

來源:互聯網
上載者:User

剛在做一個在iOS裝置上實現搖晃進而觸發一些體驗的功能,現在網上大概搜了一下,然後看了一下開發文檔,一個感觸是,網上劣質文章太多了,盲目轉載拷貝的文章也很多,重樣而且還都沒有講清實現原理,與其去網上費時搜,還不如看一下開發文檔,很快就可以解決需要解決的問題。  具體實現方法:iOS的SDK中已經將shake 事件很方便的融合進去,就像觸發touch事件一樣,需要注意的是,先給當前ViewController設定為第一響應者,然後當app發生shake事件後程式會自動執行-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event只需要在相應的觸發事件中寫需求相應的功能。 具體代碼:設定第一響應者:[cpp]  -(BOOL)canBecomeFirstResponder{      return YES;  }    -(void)viewDidAppear:(BOOL)animated{      [self canBecomeFirstResponder];  }   處理相應觸發事件:[cpp]  #pragma mark -  #pragma mark - MotionShake Event  -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{        }    -(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{      if(motion == UIEventSubtypeMotionShake){          NSLog(@"Shake!!!!!!!!!!!");      }  }    -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{        }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.