motionBegan:不工作

PF1*_*PF1 12 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;
}
Run Code Online (Sandbox Code Playgroud)

}

第一个例子.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]);
}
Run Code Online (Sandbox Code Playgroud)

}

第二个例子.h(继承自UIApplicationDelegate和UIScrollViewDelegate的类){

#import <UIKit/UIKit.h>

@class TestApplicationViewController;

@interface TestApplicationAppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {

IBOutlet UIWindow *window;
IBOutlet UIScrollView *scrollView;
}
Run Code Online (Sandbox Code Playgroud)

}

第二个例子.m {

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [self becomeFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

}

- 第二个示例返回以下警告:'TestApplicationAppDelegate'可能无法响应'-becomeFirstResponder'

Pau*_*ley 31

我假设你想在子类中实现它UIViewController.使UIViewController能够成为第一个响应:

- (BOOL)canBecomeFirstResponder {
     return YES;
}
Run Code Online (Sandbox Code Playgroud)

UIViewController成为第一响应者viewDidAppear:.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

如果视图包含UITextField可能成为第一响应者本身的任何元素(例如a),请确保该元素在某个时刻重新响应第一个响应者.UITextField例如,a UIViewController需要实现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;
}
Run Code Online (Sandbox Code Playgroud)

实行 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];
    }
}
Run Code Online (Sandbox Code Playgroud)

苹果公司的Matt Drance在iPhone开发者论坛上发表了一篇很好的文章(要求注册为开发者).

更新:在UIView的子类中实现

正如评论中所讨论的那样,这也可以在子类中使用,而不是太令人惊讶UIView.以下是构建最小示例的方法.

创建一个新的基于视图的应用程序项目,调用它ShakeTest.创建一个新的子类UIView,调用它ShakeView.让ShakeView.h这个样子的:

#import <UIKit/UIKit.h>
@interface ShakeView : UIView {
}
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

ShakeTestViewController.h这个样子的:

#import <UIKit/UIKit.h>
#include "ShakeView.h"
@interface ShakeTestViewController : UIViewController {
    ShakeView *s;
}
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

建立并运行.点击Cmd-Ctrl-Z来摇动iPhone模拟器.惊叹于日志消息.