我将以下代码添加到appDelegate.m中
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake )
{
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)shakeSuccess
{
// do something...
}
Run Code Online (Sandbox Code Playgroud)
然后我补充说:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// INIT THE BACKGROUND PATH STRING
[self refreshFields];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" object:nil];***
return YES;
}
Run Code Online (Sandbox Code Playgroud)
当我在iPhone上启动应用程序时,名为"shakeSuccess"的方法不会被调用.我应该怎么做才能在我的应用程序中实现此功能?任何的想法?
Chr*_*orr 59
这可能会对你有帮助...
/sf/answers/168398471/
他说,你应该设置UIApplication的applicationSupportsShakeToEdit到
YES.并覆盖VC中的3个方法:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
Run Code Online (Sandbox Code Playgroud)
你的其余代码很好.(-motionEnded:withEvent:)
小智 26
你可以这样做......
首先...
在App的Delegate中设置applicationSupportsShakeToEdit属性:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.applicationSupportsShakeToEdit = YES;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
Run Code Online (Sandbox Code Playgroud)
其次...
在视图控制器中添加/覆盖canBecomeFirstResponder,viewDidAppear:和viewWillDisappear:方法:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
Run Code Online (Sandbox Code Playgroud)
第三...
将motionEnded方法添加到View Controller:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
// your code
}
}
Run Code Online (Sandbox Code Playgroud)
如果第一个答案没有,这应该工作,这只是快速键入未经测试:)
Che*_*eny 14
如果您希望能够在应用程序中检测到抖动,最好的方法是使用自定义类覆盖应用程序的类.
然后在自定义应用程序类中实现它
@implementation PSApplication
- (void)sendEvent:(UIEvent *)event
{
if ( event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake ) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"shakeNotification" object:nil];
}
[super sendEvent:event];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33295 次 |
| 最近记录: |