运动开始:不工作 [英] motionBegan: Not Working

查看:18
本文介绍了运动开始:不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

感谢您的帮助!

第一个示例 .h(从 UIView 继承的类 - 从应用程序委托类调用"){

First Example .h (class inherited from UIView - Is "called" from the app delegate class) {

@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 的类){

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;
}

}

第二个例子.m{

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [self becomeFirstResponder];
}

}

-- 第二个示例返回以下警告:TestApplicationAppDelegate"可能不会响应-becomeFirstResponder"

-- The second example returns the following warning: 'TestApplicationAppDelegate' may not respond to '-becomeFirstResponder'

推荐答案

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

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

- (BOOL)canBecomeFirstResponder {
     return YES;
}

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

Make the UIViewController become first responder in viewDidAppear:.

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

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

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;
}

实现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 开发者论坛中有一个 Apple 的 Matt Drance 的好帖子(需要注册为开发者).

There is a good post by Matt Drance of Apple in the iPhone Developer Forums (requires registration as a developer).

正如评论中所讨论的,这也适用于 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.

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

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

使 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 模拟器.惊叹于日志消息.

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

这篇关于运动开始:不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆