MotionBegan:不工作

发布于 2024-08-03 07:45:45 字数 1309 浏览 6 评论 0原文

当我尝试使用 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-08-10 07:45:45

我假设您想在 UIViewController 的子类中实现它。使 UIViewController 能够成为第一响应者:

- (BOOL)canBecomeFirstResponder {
     return YES;
}

使 UIViewController 成为 viewDidAppear: 中的第一响应者。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

如果视图包含任何可能成为第一响应者本身的元素(例如 UITextField),请确保该元素在某个时刻放弃第一响应者。例如,对于 UITextFieldUIViewController 需要实现 UITextFieldDelegate 协议,并且实际上是委托。然后,在 textFieldShouldReturn:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    // Hides the keyboard
    [theTextField resignFirstResponder];
    // Returns first responder status to self so that shake events register here
    [self becomeFirstResponder];
    return YES;
}

实现 motionEnded:withEvent:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
    // Do something
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
        [super motionEnded:motion withEvent:event];
    }
}

中有一个 Apple 的 Matt Drance 在 iPhone 开发者论坛中发布的好帖子(需要注册为开发者)。

更新:在 UIView 的子类中实现

正如评论中所讨论的,这在 UIView 的子类中也可以工作,这并不奇怪。以下是如何构建一个最小的示例。

创建一个新的基于视图的应用程序项目,将其命名为ShakeTest。创建 UIView 的新子类,将其命名为 ShakeView。让 ShakeView.h 看起来像这样:

#import <UIKit/UIKit.h>
@interface ShakeView : UIView {
}
@end

ShakeView.m 看起来像这样:

#import "ShakeView.h"
@implementation ShakeView
- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
        NSLog(@"Shake!");
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
        [super motionEnded:motion withEvent:event];
    }
}
@end

ShakeTestViewController.h 看起来像这样:

#import <UIKit/UIKit.h>
#include "ShakeView.h"
@interface ShakeTestViewController : UIViewController {
    ShakeView *s;
}
@end

ShakeTestViewController .m 如下所示:

#import "ShakeTestViewController.h"
@implementation ShakeTestViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    s = [[ShakeView alloc] init];
    [[self view] addSubview:s];
    [s becomeFirstResponder];
}
- (void)dealloc {
    [s release];
    [super dealloc];
}
@end

构建并运行。按 Cmd-Ctrl-Z 摇动 iPhone 模拟器。惊叹于日志消息。

I assume you want to implement this in a subclass of UIViewController. Make the UIViewController capable of becoming first responder:

- (BOOL)canBecomeFirstResponder {
     return YES;
}

Make the UIViewController become first responder in viewDidAppear:.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

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 a UITextField, for example, the UIViewController would need to implement the UITextFieldDelegate protocol, and actually be the delegate. Then, in textFieldShouldReturn:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    // Hides the keyboard
    [theTextField resignFirstResponder];
    // Returns first responder status to self so that shake events register here
    [self becomeFirstResponder];
    return YES;
}

Implement motionEnded:withEvent:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
    // Do something
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
        [super motionEnded:motion withEvent:event];
    }
}

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 of UIView, call it ShakeView. Make ShakeView.h look like this:

#import <UIKit/UIKit.h>
@interface ShakeView : UIView {
}
@end

Make ShakeView.m look like this:

#import "ShakeView.h"
@implementation ShakeView
- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ( event.subtype == UIEventSubtypeMotionShake ) {
        NSLog(@"Shake!");
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)]) {
        [super motionEnded:motion withEvent:event];
    }
}
@end

Make ShakeTestViewController.h look like this:

#import <UIKit/UIKit.h>
#include "ShakeView.h"
@interface ShakeTestViewController : UIViewController {
    ShakeView *s;
}
@end

Make ShakeTestViewController.m look like this:

#import "ShakeTestViewController.h"
@implementation ShakeTestViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    s = [[ShakeView alloc] init];
    [[self view] addSubview:s];
    [s becomeFirstResponder];
}
- (void)dealloc {
    [s release];
    [super dealloc];
}
@end

Build and run. Hit Cmd-Ctrl-Z to shake the iPhone simulator. Marvel at the log message.

请远离我 2024-08-10 07:45:45

你有实施旧的方法吗

'加速度计:didAccelerate'

?如果您实现了旧的应用程序范围方法,则需要在调用新方法之前将其删除。

Do you have the old method implemented

'acelerometer:didAccelerate'

? If you have the old application wide method implemented, you need to remove it before the new one will be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文