开发者

CGPoint from UITapGestureRecognizer

I need to get the x and y position of a UITapG开发者_开发技巧estureRecognizer, but in doing so my application crash

This is the code where I create the Recognizer

-(void)imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info
{

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    UIImage * image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [image drawInRect:CGRectMake(0,0, 200, 400)];

    MyImg =[[UIImageView alloc] initWithImage:image]; 

    UITapGestureRecognizer *recognizer;

    MyImg.userInteractionEnabled=YES;

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getTouchColor:)];
    [MyImg addGestureRecognizer:recognizer];    
    [recognizer release];

    [self.view addSubview:MyImg];

    [picker release];

}

And my event GetTouchColor

-(void)getTouchColor:(UITapGestureRecognizer *) recognizer
{
    if (recognizer.state==UIGestureRecognizerStateEnded)
    {

    CGPoint point = [recognizer locationInView:MyImg];

        NSLog(@"%@", NSStringFromCGPoint(point));
}

If I remove the line

 CGPoint point = [recognizer locationInView:MyImg];

The code works perfectly and the application does not crash.

What am I doing wrong?

Thanks

/ / Sorry my english from google


Check that recognizer is not nil. Sending messages to nil usually returns nil or 0, depending on the return type, but when it’a a struct return type, the result is undefined, and may (among other things) cause a crash.


First thing, It seems, is you have missed to set the frame of MyImg.MyImg.


I believe that MyImg variable is only declared in imagePickerController therefore is not reachable in getTouchColor, you need to include a reference to that view when calling getTouchColor, otherwise it won't recognize MyImg.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜