iPhone搖一搖示例代碼:ShakeDemo
ShakeDemo
搖一搖示例代碼。
前言 Foreword
iPhone上的搖一搖實現很簡單。具體參考下面的代碼或者下載Demo試一下。
代碼 Code
-
在需要響應搖一搖的controller中,設置canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder { return YES; }
直接實現下面的代理方法即可:
#pragma mark - 搖動 /** * 搖動開始 */ - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion == UIEventSubtypeMotionShake) { NSLog(@"開始搖了"); } } /** * 搖動結束 */ - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"搖動結束"); } /** * 搖動取消 */ - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"搖動取消"); }
- 模擬器調試技巧:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!