PDA

View Full Version : How Do You Store a Coordinate Pair generated during a touch event?




happybob
Apr 18, 2009, 05:53 PM
Simply, I'm trying to rotate an image about its center, as a person drags their finger around the center. Though the twist is that it rotates in 30 degree increments. It also must know it's rotational location in respect to it's starting orientation.

I'm trying to create a rotating object for the iPhone. I have it calculating all the trig. The problem is that for my project I have need to have the program reset the x and y every time a finger is dragged more over then 30 degrees around the center point. Whenever I link a variable to the x and y of the current location within the touchesMoved function they link permanently and continually change along with the drag event instead of staying constant and updating every 30 degrees. Is there a way to statically store an x and y?

Thanks Everyone.

Comment:
Tried using CGPointMake and it still linked and updated.
Code:
currentpoint = [touch locationInView:self.view];
when angle between startpoint and currentpoint >= 30 degrees
set altpoint = currentpoint,
I did that using CGPointMake and it didn't work.
altpoint continually updated to currentpoint



BlackWolf
Apr 18, 2009, 08:32 PM
Simply, I'm trying to rotate an image about its center, as a person drags their finger around the center. Though the twist is that it rotates in 30 degree increments. It also must know it's rotational location in respect to it's starting orientation.

I'm trying to create a rotating object for the iPhone. I have it calculating all the trig. The problem is that for my project I have need to have the program reset the x and y every time a finger is dragged more over then 30 degrees around the center point. Whenever I link a variable to the x and y of the current location within the touchesMoved function they link permanently and continually change along with the drag event instead of staying constant and updating every 30 degrees. Is there a way to statically store an x and y?

Thanks Everyone.

Comment:
Tried using CGPointMake and it still linked and updated.
Code:
currentpoint = [touch locationInView:self.view];
when angle between startpoint and currentpoint >= 30 degrees
set altpoint = currentpoint,
I did that using CGPointMake and it didn't work.
altpoint continually updated to currentpoint

I guess that something logical in your code is wrong, because when I store the position of a touch via locationInView: it doesn't auto-update (and it actually can't do that because it is not an object so you can't create a reference!)
You must set the starting point of the touch in an instance variable in touchStarted:withEvent: ! and then you have to check for the current position in touchMoved:withEvent: ... if you do that, it should work fine. if it doesn't, please post your code (the important stuff)

happybob
Apr 18, 2009, 11:57 PM
Thanks for responding. Here's some of the code. If you have any idea where I'm going wrong please let me know. Thanks in advance.

All the equations and calculation are located within the touchesMoved function and do not call any outside functions

#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *firsttouch = [touches anyObject];
gestureStartPoint = [firsttouch locationInView:self.view];

***Above I define the start point***
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
currentLocation = [touch locationInView:self.view];
float... ***Define all the floats***
currentx = currentLocation.x;
currenty = currentLocation.y;
centerX = ***Set Point***
centerY = ***Set Point***
startx = gestureStartPoint.x;
starty = gestureStartPoint.y;

***Then calculate the length of each side that form the triangle from the center to the touch origin and ending at the current location***

***Use law of cosines to calculate the angle between the origin and the current location***

if( cosangle >= 30) {
altpoint = CGPointMake(currentx,currenty);
}

***The above is where I've been trying to reset the origin point when the angle is equal to 30***

***Display all the information: position, angle, etc***
}

BlackWolf
Apr 19, 2009, 04:43 AM
1) please put your code in tags, makes it much easier to read :D

2) mmh ... I don't really have ideas really, you could try
[code]
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *firsttouch = [[touches anyObject] copy];
gestureStartPoint = [firsttouch locationInView:self.view];

***Above I define the start point***
}


but I really think your code should work. I have the exact same code (except for the angle stuff) in my application and it works fine.
for debugging, try the following:
put this at the end of your touchesBegan:withEvent: method and at the beginning of touchesMoved:withEvent:

NSLog(@"%f , %f",gestureStartPoint.x, gestureStartPoint.y);


then make a touch and move it around a little, then see what this printed to your console.

happybob
Apr 20, 2009, 12:14 PM
Thanks, you were completely right. My logic was just screwed in one place.