MotionBegan:不工作
当我尝试使用 (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 来捕获摇动事件时,我遇到了一些问题。问题是该函数甚至没有运行,即使我覆盖 canBecomeFirstResponder 并将其设置为返回 YES 也是如此。我看过其他人的帖子有这个问题,但我还没有找到答案。
感谢您的帮助!
第一个示例 .h(从 UIView 继承的类 - 从应用程序委托类“调用”) {
@class TestApplicationView;
@interface TestApplicationView : UIView {
IBOutlet UIView *view;
}
}
第一个示例 .m {
- (id)initWithCoder:(NSCoder *)coder
{
[self setUpView];
return self;
}
- (id)initWithFrame:(CGRect)frame
{
[self setUpView];
return self;
}
- (void)setUpView
{
[self becomeFirstResponder];
NSLog(@"First Responder - %d", [self isFirstResponder]);
}
}
第二个示例 .h(从 UIApplicationDelegate 和 UIScrollViewDelegate 继承的类) {
#import <UIKit/UIKit.h>
@class TestApplicationViewController;
@interface TestApplicationAppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
IBOutlet UIWindow *window;
IBOutlet UIScrollView *scrollView;
}
}
第二个示例 .m {
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[self becomeFirstResponder];
}
}
-- 第二个示例返回以下警告:“TestApplicationAppDelegate”可能不会响应“-becomeFirstResponder”
I am running into a bit of a problem when I attempt to use (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event in order to capture a shake event. The problem is that the function isn't even running, even when I override canBecomeFirstResponder and set it to return YES. I have seen some other people's posts with this problem, but I have not found an answer.
Thanks for any help!
First Example .h (class inherited from UIView - Is "called" from the app delegate class)
{
@class TestApplicationView;
@interface TestApplicationView : UIView {
IBOutlet UIView *view;
}
}
First Example .m
{
- (id)initWithCoder:(NSCoder *)coder
{
[self setUpView];
return self;
}
- (id)initWithFrame:(CGRect)frame
{
[self setUpView];
return self;
}
- (void)setUpView
{
[self becomeFirstResponder];
NSLog(@"First Responder - %d", [self isFirstResponder]);
}
}
Second Example .h (class inherited from UIApplicationDelegate and UIScrollViewDelegate)
{
#import <UIKit/UIKit.h>
@class TestApplicationViewController;
@interface TestApplicationAppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
IBOutlet UIWindow *window;
IBOutlet UIScrollView *scrollView;
}
}
Second Example .m
{
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[self becomeFirstResponder];
}
}
-- The second example returns the following warning: 'TestApplicationAppDelegate' may not respond to '-becomeFirstResponder'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您想在 UIViewController 的子类中实现它。使
UIViewController
能够成为第一响应者:使
UIViewController
成为viewDidAppear:
中的第一响应者。如果视图包含任何可能成为第一响应者本身的元素(例如
UITextField
),请确保该元素在某个时刻放弃第一响应者。例如,对于UITextField
,UIViewController
需要实现UITextFieldDelegate
协议,并且实际上是委托。然后,在textFieldShouldReturn:
实现
motionEnded:withEvent:
中有一个 Apple 的 Matt Drance 在 iPhone 开发者论坛中发布的好帖子(需要注册为开发者)。
更新:在 UIView 的子类中实现
正如评论中所讨论的,这在
UIView
的子类中也可以工作,这并不奇怪。以下是如何构建一个最小的示例。创建一个新的基于视图的应用程序项目,将其命名为
ShakeTest
。创建UIView
的新子类,将其命名为ShakeView
。让ShakeView.h
看起来像这样:让
ShakeView.m
看起来像这样:让
ShakeTestViewController.h
看起来像这样:让
ShakeTestViewController .m
如下所示:构建并运行。按 Cmd-Ctrl-Z 摇动 iPhone 模拟器。惊叹于日志消息。
I assume you want to implement this in a subclass of
UIViewController
. Make theUIViewController
capable of becoming first responder:Make the
UIViewController
become first responder inviewDidAppear:
.If the view contains any element, such as a
UITextField
, that might become first responder itself, ensure that element resigns first responder at some point. With aUITextField
, for example, theUIViewController
would need to implement theUITextFieldDelegate
protocol, and actually be the delegate. Then, intextFieldShouldReturn:
Implement
motionEnded:withEvent:
There is a good post by Matt Drance of Apple in the iPhone Developer Forums (requires registration as a developer).
Update: implementing in a subclass of UIView
As discussed in the comments, this also works, not too surprisingly, in a subclass of
UIView
. Here's how to construct a minimal example.Create a new View-based Application project, call it
ShakeTest
. Create a new subclass ofUIView
, call itShakeView
. MakeShakeView.h
look like this:Make
ShakeView.m
look like this:Make
ShakeTestViewController.h
look like this:Make
ShakeTestViewController.m
look like this:Build and run. Hit Cmd-Ctrl-Z to shake the iPhone simulator. Marvel at the log message.
你有实施旧的方法吗
?如果您实现了旧的应用程序范围方法,则需要在调用新方法之前将其删除。
Do you have the old method implemented
? If you have the old application wide method implemented, you need to remove it before the new one will be called.