IOS端的搖一搖功能

gxw6 9年前發布 | 2K 次閱讀 C/C++ Objective-C

在 UIResponder中存在這么一套方法

  • (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event OSX_AVAILABLE_STARTING(MAC_NA,__IPHONE_3_0);

  • (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event OSX_AVAILABLE_STARTING(MAC_NA,__IPHONE_3_0);

  • (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event OSX_AVAILABLE_STARTING(MAC_NA,__IPHONE_3_0);

這就是執行搖一搖的方法。那么怎么用這些方法呢?

很簡單,你只需要讓這個Controller本身支持搖動

同時讓他成為第一相應者:

  • (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];

[selfbecomeFirstResponder];

}

然后去實現那幾個方法就可以了

  • (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//檢測到搖動

}

  • (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//搖動取消

}

  • (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

//搖動結束

if (event.subtype == UIEventSubtypeMotionShake) {

    //something happens

}

}

</pre>

 本文由用戶 gxw6 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!