我需要获取UITapGestureRecognizer的x和y位置,但这样做会导致应用程序崩溃
这是我创建识别器的代码
-(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];
}
我的事件GetTouchColor
-(void)getTouchColor:(UITapGestureRecognizer *) recognizer
{
if (recognizer.state==UIGestureRecognizerStateEnded)
{
CGPoint point = [recognizer locationInView:MyImg];
NSLog(@"%@", NSStringFromCGPoint(point));
}
如果我删除该行
CGPoint point = [recognizer locationInView:MyImg];
代码完美运行,应用程序不会崩溃。
我做错了什么?
由于
/ /抱歉,我的英语来自谷歌
答案 0 :(得分:2)
检查recognizer
是否不是nil
。向nil
发送消息通常会返回nil
或0,具体取决于返回类型,但是当它是结构返回类型时,结果是未定义的,并且可能(除其他外)导致崩溃。
答案 1 :(得分:0)
首先,您似乎错过了设置frame
的{{1}}。
答案 2 :(得分:0)
我认为MyImg变量仅在imagePickerController
中声明,因此在getTouchColor
中无法访问,您需要在调用getTouchColor
时包含对该视图的引用,否则它不会认可MyImg
。