当我试图找到一个人触摸屏幕时,我的getter方法遇到了问题。
在我的.h文件中,我有属性
@property(nonatomic, assign)CGPoint touchLocation;
在我的.m文件中,我有以下代码
@synthesize touchLocation=_touchLocation;
-(void)userTouchLocation
{
recognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationPressedCoordinates:)];
[recognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:recognizer];
}
-(void)locationPressedCoordinates:(UITapGestureRecognizer *)rec
{
CGPoint loc= [rec locationInView:rec.view];
[self setTouchLocation:loc];
}
-(void)setTouchLocation:(CGPoint)touchLocation
{
_touchLocation=touchLocation;
}
-(CGPoint)touchLocation
{
return _touchLocation;
}
然而,当我尝试通过做类似的事情获得位置时 CGPoint foo = [self touchLocation];它返回点(0,0)。如果我使用NSLog语句来调试我的程序,我的setter方法可以工作,但getter方法不行。