The iphone has a good support for shaking, generally two steps: first in the attempt to open the controller to accept the shaking of the switch and then in the method of shaking the trigger to achieve the function you want to achieve.
在 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];
//Turn on shake function
[[UIApplicationsharedApplication]setApplicationSupportsShakeToEdit:YES];
//Make current controller a first responder[ Self
becomeFirstResponder];
And then go and implement those methods.-(void) Motionbegan: (Uieventsubtype) Motionwithevent: (Uievent
*)event
{
//检测到摇动
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//摇动取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
//摇动结束
if(event.subtype == UIEventSubtypeMotionShake) {
//somethinghappens
}
}
iOS shake-shake function implementation