omr*_*mri 2 iphone objective-c
我对之前发布的震动检测有疑问,这是一个提醒:
"现在......我想做类似的事情(在iPhone OS 3.0+中),只是在我的情况下我想在应用程序范围内,所以我可以在发生震动时提醒应用程序的各个部分.这就是我最终做的事情.
首先,我将UIWindow子类化.这很容易.使用MotionWindow:UIWindow等界面创建一个新的类文件(随意选择自己的,natch).添加如下方法:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceShaken" object:self];
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您使用MainWindow.xib(库存Xcode模板的东西),请进入并将Window对象的类从UIWindow更改为MotionWindow或您调用它的任何内容.保存xib.如果以编程方式设置UIWindow,请在那里使用新的Window类.
现在您的应用程序正在使用专门的UIWindow类.无论您想要被告知摇晃,请注册他们的通知!像这样:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil];
Run Code Online (Sandbox Code Playgroud)
要以观察者身份移除自己:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
问题:
谢谢.
在iPhone OS 3.x中,从任何设置为第一响应者的视图接收动作事件都很简单.
在您的视图中重写方法motionEnded:
,如下所示:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake && [self isViewLoaded])
{
//handle shake here...
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您需要在加载并显示视图时成为第一响应者:
- (void)viewDidAppear:(BOOL)animated
{
[self becomeFirstResponder];
[super viewDidAppear:animated];
//any extra set up code...
}
Run Code Online (Sandbox Code Playgroud)
您可能还必须回应该canBecomeFirstResponder
方法.
- (BOOL)canBecomeFirstResponder
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这些可以在任何继承UIView形式的对象中使用.
归档时间: |
|
查看次数: |
1084 次 |
最近记录: |