1. When the device shakes, the system calculates the accelerometer value and tells whether a shake gesture has occurred. The system will only notify you when the movement starts and ends, and will not always report every movement to you throughout the course of the exercise. For example, if you shake the device three times quickly, you will only receive a shake event.
2, to implement a shake gesture, you first need to make the view controller the first responder, note that it is not a separate control. The most appropriate time to become the first responder is to release the first responder when the view is present and when the view disappears.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-(
BOOL
)canBecomeFirstResponder {
return YES
; } -(
void
)viewDidAppear:(
BOOL
)animated {
[
super viewDidAppear:animated];
[
self becomeFirstResponder]; }
-(
void
)viewWillDisappear:(
BOOL
)animated {
[
self resignFirstResponder];
[
super viewWillDisappear:animated]; }
|
The view controller is now ready to handle motion events, with three methods, motionbegan,motionended and motioncancelled, that work in a similar way to touch methods, but with no moving methods.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (
void
)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake)
{
NSLog
(@
"Shake Began"
);
} }
-(
void
)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake)
{
NSLog
(@
"Shake End"
);
} }
-(
void
)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake)
{
NSLog
(@
"Shake Cancelled"
);
} }
|
The above is reproduced from Http://blog.csdn.net/marvindev
My Code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// // ocqViewController.m // shake // // Created by ocq on 14-4-29. // Copyright (c) 2014年 ocq. All rights reserved. //
#import "ocqViewController.h"
@interface ocqViewController ()
@end
@implementation ocqViewController
- (
void
)viewDidLoad {
[
super viewDidLoad];
NSLog
(@
"初始化了实例"
);
// Do any additional setup after loading the view, typically from a nib. }
- (
void
)didReceiveMemoryWarning {
[
super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
-(
BOOL
)canBecomeFirstResponder {
return YES
; }
-(
void
)viewDidAppear:(
BOOL
)animated {
[
super viewDidAppear:animated];
[
self becomeFirstResponder]; }
-(
void
)viewWillDisappear:(
BOOL
)animated {
[
self resignFirstResponder];
[
super viewWillDisappear:animated]; }
////当手机移动结束时调用该方法 #pragma motion移动的类型 UIEvent事件 - (
void
)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//如果是摇手机类型的事件
if
(motion==UIEventSubtypeMotionShake){
|
?
?
1 |
//创建并初始化对话框 UIAlertView *show=[[UIAlertView alloc] initWithTitle:@"ocq ios 学习" message:@"恭喜,您查找到了好友" delegate:nil cancelButtonTitle:@"添加为好友" otherButtonTitles: nil]; //显示对话框 [show show]; // [show release]; <br>} <br>} <br>@end<br><br> |