尝试设置手势识别时崩溃

时间:2012-09-14 22:36:51

标签: iphone ios ipad uigesturerecognizer

我正在尝试在我的代码中设置一个轻击手势,当我尝试确定坐标时,我遇到了崩溃。任何帮助将不胜感激。

2012-09-14 17:25:49.149 valhalla[16469:707] tap detected
2012-09-14 17:25:49.159 valhalla[16469:707] -[UITapGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x37bf10
2012-09-14 17:25:49.165 valhalla[16469:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer translationInView:]: unrecognized selector sent to instance 0x37bf10'
*** First throw call stack:
(0x3104c88f 0x3611d259 0x3104fa9b 0x3104e915 0x3104f788 0x7a4a1 0x31870637 0x31800d65 0x31a31479 0x3177cf55 0x3177baa3 0x317887e9 0x31788627 0x317881f5 0x3176e695 0x3176df3b 0x313e222b 0x31020523 0x310204c5 0x3101f313 0x30fa24a5 0x30fa236d 0x313e1439 0x3179ccd5 0x75307 0x752c8)




#import "GLViewController.h"
#import "GLView.h"

@implementation GLViewController

- (void)loadView {

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

    GLView *glView = [[[GLView alloc] initWithFrame:applicationFrame] autorelease];
    self.view = glView;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self action:@selector(handleSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // We can run in landscape mode
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

/*- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //[self.view becomeFirstResponder];
    //[self.view setReturnKeyType:UIReturnKeyDone];
}*/
- (void)didReceiveMemoryWarning
{ 
    // default behavior is to release the view if it doesn't have a superview.

    // remember to clean up anything outside of this view's scope, such as
    // data cached in the class instance and other global data.
    [super didReceiveMemoryWarning];
}

- (void) handleSingleTap: (UIPanGestureRecognizer *) uigr
{
    NSLog(@"tap detected");
    CGPoint translation = [uigr translationInView:self.view];
    CGPoint finalPosition = self.view.center;
    finalPosition.x += translation.x;
    finalPosition.y += translation.y;

}

@end

2 个答案:

答案 0 :(得分:5)

您声明了UITapGestureRecognizer,但您的方法正在转换为UIPanGestureRecognizer

- (void) handleSingleTap: (UIPanGestureRecognizer *) uigr

UITapGestureRecognizer未声明translationInView。这就是你的应用程序崩溃的原因。

答案 1 :(得分:2)

您希望使用locationInView:而不是translationInView: