motionBegan没有被整个应用程序调用

时间:2011-03-15 22:14:15

标签: iphone facebook post shake first-responder

大家好 我有一个应用程序,我允许用户在他们的Facebook的pinwall上发布一些东西。我直接从Facebook使用DemoApp进行设置。

我已经实施了一种震动检测,可以在用户摇动设备时执行某些操作。但是,由于这两个函数位于同一个viewcontroller中,因此它们都不再起作用了。

  • 当Facebook弹出登录窗口时,键盘不再出现。
  • 不再检测到抖动。

我想这与第一响应者有关。我尝试了很多东西,但我无法解决它。这是我的代码中必不可少的部分。

在facebook pinwall上发布一些内容:

- (IBAction)publishStream{

    FactsAppDelegate *appDelegate = (FactsAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init]autorelease];
    [dictionary setObject:@"Du postest diesen Fact:" forKey:@"user_message_prompt"];
    [dictionary setObject:[[appDelegate.facts objectAtIndex:currentFactId] fact] forKey:@"message"];
    [_facebook dialog:@"feed" andParams:dictionary andDelegate:self];
}

摇晃检测:

#pragma mark Motion catching
// shake motion began
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion != UIEventSubtypeMotionShake) return;

    // load next fact
    [self next];

    // vibrate
    [self vibrate];
}

震动检测所需的方法:

#pragma mark Shake events are only detectable by the first responder
- (BOOL)canBecomeFirstResponder {
    return YES;
}

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
}

任何人都可以告诉我我必须改变什么才能让两个(脸书,摇晃)工作? 非常感谢, doonot

3 个答案:

答案 0 :(得分:1)

我在XCode中创建了一个基于视图的简单项目,以下内容适用于我。我没有添加Facebook到混合,但UITextField应该模拟它。这样做是:

  1. 触摸UIButton或摇动设备将切换UITextField的编辑,包括显示/隐藏键盘

  2. UILabel statusLabel将显示最后一次触摸或摇动动作,然后超时显示默认文字。

  3. 您可以创建一个类似的项目并将其复制/粘贴或尝试使用现有的应用程序。

    编辑添加.H文件

    部首:

    // ShakeShakeShakeViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface ShakeShakeShakeViewController : UIViewController {
        UITextField *   _textField;
        UILabel *       _statusLabel;
        UIButton *      _actionButton;
    }
    
    @property (nonatomic, retain)       IBOutlet UITextField *  textField;
    @property (nonatomic, retain)       IBOutlet UILabel *      statusLabel;
    @property (nonatomic, retain)       IBOutlet UIButton *     actionButton;
    
    - (IBAction)userInvokedActionWithControl:(id)sender;
    - (void)resetStatusLabel;
    - (void)toggleTextFieldEditing;
    
    @end
    

    来源:

    // ShakeShakeShakeViewController.m
    
    #import "ShakeShakeShakeViewController.h"
    
    @implementation ShakeShakeShakeViewController
    
    @synthesize textField = _textField,
                statusLabel = _statusLabel,
                actionButton = _actionButton;
    
    - (void)dealloc
    {
        self.textField = nil;
        self.statusLabel = nil;
        self.actionButton = nil;
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [self resetStatusLabel];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        self.textField = nil;
        self.statusLabel = nil;
        self.actionButton = nil;
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [self becomeFirstResponder];
    }
    
    - (IBAction)userInvokedActionWithControl:(id)sender
    {
        NSLog(@"Touched");
        self.statusLabel.text = @"Touched";
        [self toggleTextFieldEditing];
    }
    
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 
    {
        if (motion != UIEventSubtypeMotionShake) return;
    
        NSLog(@"Shaking...");
        self.statusLabel.text = @"Shaking...";
    }
    
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if (motion != UIEventSubtypeMotionShake) return;
        NSLog(@"Done shaking...");
        self.statusLabel.text = @"Done shaking";
        [self toggleTextFieldEditing];
    }
    
    - (void)toggleTextFieldEditing
    {
        if ([self.textField isFirstResponder]) {
            [self becomeFirstResponder];
        }
        else {
            [self.textField becomeFirstResponder];
        }
        [self performSelector:@selector(resetStatusLabel) withObject:nil afterDelay:2];
    }
    
    - (void)resetStatusLabel
    {
        self.statusLabel.text = @"Shake me";
    }
    
    @end
    

答案 1 :(得分:0)

尝试在viewDidAppear

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

这里是documentation

答案 2 :(得分:0)

感谢XJones,我终于能够解决我的问题了。

@XJones,我正在将我的应用程序缩减为默认功能,并找出了为什么我无法捕捉动作以及为什么我的facebook键盘出现问题。

我的MainWindow.xib文件完全错误,并且未设置委托。我删除了xib文件,映射了委托和窗口,就是这样,我现在可以抓住动作甚至facebook发布的作品就像魅力一样:)

非常感谢您的关心和提供帮助!!