I'm getting started with Objective-C, and there is something about variables scope that I still didn't get. I searched about it, but I still couldn't catch what I'm doing wrong.
I'm trying to create a code that will give me the x,y difference between two point. The first NSLog inside the first IF shows the right value for pointWhereDragBegan.x and .y, but when I try to get the value of the pointWhereDragBegan in the second IF statement, the value I get for pointWhereDragBegan.x is -1.998683 and .y is 0.0.
I'm sure it is something really simple, I just can't catch my mistake.
- (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);
}
}
pointWhereDragBeganin the secondifstatement. You're only loggingpointWhereDragEndedand the results of the subtractions.