我开始使用Objective-C,关于变量范围的一些东西我仍然没有得到。我搜索了一下,但我仍然无法抓住我做错了什么。
我正在尝试创建一个代码,它会给我两点之间的 x,y 差异。第一个 IF 中的第一个 NSLog 显示了 pointWhereDragBegan.x 和 .y 的正确值,但是当我尝试在第二个 IF 语句中获取 pointWhereDragBegan 的值时,我得到的 pointWhereDragBegan.x 的值是 -1.998683 和 .y为 0.0。
我确定这很简单,我只是无法理解我的错误。
- (void)drag:(UILongPressGestureRecognizer *)drag{
CGPoint pointWhereDragBegan;
if(drag.state == UIGestureRecognizerStateBegan){
pointWhereDragBegan = [drag locationInView:self];
NSLog(@"Drag started at %f,%f",pointWhereDragBegan.x,pointWhereDragBegan.y);
}
if(drag.state == UIGestureRecognizerStateEnded){
CGPoint pointWhereDragEnded = [drag locationInView:self];
float xDragged = pointWhereDragEnded.x - pointWhereDragBegan.x;
float yDragged = pointWhereDragEnded.y - pointWhereDragBegan.y;
NSLog(@"Drag ended at %f,%f",pointWhereDragEnded.x,pointWhereDragEnded.y);
NSLog(@"The user moved %f, %f",xDragged,yDragged);
}
}