在 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
}
}
IOS实现摇一摇源代码
- @interface ShakeViewController : UIViewController<UIAccelerometerDelegate>
- {
- UIAccelerationValue myAccelerometer[3];
- //是否响应摇一摇的标志
- BOOL _canShake;
- }
.m文件中
- #define kFilteringFactor 0.1
- #define kEraseAccelerationThreshold 2.0
- @implementation ShakeViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- _canShake = YES;
- }
- return self;
- }
- - (void)dealloc
- {
- [UIAccelerometer sharedAccelerometer].delegate = nil;
- [super dealloc];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [UIAccelerometer sharedAccelerometer].delegate = self;
- [UIAccelerometer sharedAccelerometer].updateInterval = 1.0f/40.0f;
- }
- #pragma mark - UIAccelerometerDelegate
- - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
- {
- UIAccelerationValue length, x, y, z;
- if (!_canShake)
- {
- return;
- }
- //Use a basic high-pass filter to remove the influence of the gravity
- myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
- myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
- myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
- // Compute values for the three axes of the acceleromater
- x = acceleration.x - myAccelerometer[0];
- y = acceleration.y - myAccelerometer[0];
- z = acceleration.z - myAccelerometer[0];
- //Compute the intensity of the current acceleration
- length = sqrt(x * x + y * y + z * z);
- // If above a given threshold, play the erase sounds and erase the drawing view
- if(length >= kEraseAccelerationThreshold)
- {
- //是否响应摇一摇的标志
- _canShake = NO;
- [self shakeEvent];
- }
- }
1。 在App's Delegate中设定applicationSupportsShakeToEdit属性:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
application.applicationSupportsShakeToEdit= YES; //在ios6.0后,这里其实都可以不写了
self.window= [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.viewController= [[ViewControlleralloc] initWithNibName:@"ViewController"bundle:nil];
self.window.rootViewController= self.viewController;
[self.windowmakeKeyAndVisible];
returnYES;
}
2。在你的View控制器中添加/重载canBecomeFirstResponder, viewDidAppear:以及viewWillDisappear:
//这里很重要,因为大部分视图 默认 的 canBecomeFirstResponder 是 NO的
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
3。在你的view控制器中添加motionEnded:
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
if(motion == UIEventSubtypeMotionShake)
{
// your code
}
}
---------------------------------------------------------------
IOS 3.0 + 开始支持motion事件,检测设备摇动
– motionBegan:withEvent: 摇动开始时执行
– motionEnded:withEvent: 摇动结束时执行
– motionCancelled:withEvent: 摇动被取消时执行