motionBegan:不工作 [英] motionBegan: Not Working

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

问题描述

当我尝试使用(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)事件来捕获摇动事件时,我遇到了一个问题。问题是,该函数甚至没有运行,即使我重写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
{

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

}

第二个例子.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
{

Second Example .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 ,它可能成为第一个响应者本身,确保元素在某个时候重新签署第一个响应者。例如,使用 UITextField UIViewController 将需要实现 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开发者论坛(需要注册为开发人员)。

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.

创建一个新的基于View的应用程序项目,调用 ShakeTest 。创建 UIView 的新子类,调用 ShakeView 。 Make 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

Make ShakeView.m 看起来像这样:

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

使 ShakeTestViewController.h 看起来像这样:

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

Make ShakeTestViewController.m 看起来像这样:

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

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

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

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

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