motionBegan:不工作

时间:2009-08-27 17:46:55

标签: iphone cocoa cocoa-touch motion

当我尝试使用(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)事件以捕获抖动事件时,我遇到了一些问题。问题是该函数甚至没有运行,即使我覆盖canBecomeFirstResponder并将其设置为返回YES。我已经看到其他人的帖子有这个问题,但我还没有找到答案。

感谢您的帮助!

第一个例子.h(继承自UIView的类 - 从app delegate类“调用”) {

@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'

2 个答案:

答案 0 :(得分:31)

我假设你想在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];
    }
}

iPhone开发者论坛中有good post by Matt Drance of Apple(需要注册为开发人员)。

更新:在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模拟器。惊叹于日志消息。

答案 1 :(得分:0)

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