Hello,
I am writing an application in which the position of an on screen object is controlled by one finger, and the position of another object is controlled by another finger. Wherever the two fingers move (not necessarily together) the objects follow. This is my code
However, this does not work well when both fingers are moving at the same time. If I move the two objects very slowly then there are no problems. But if I moved them any quicker then one object might not follow a finger, or the motion of an object, or both objects, might not be smooth etc.
Any suggestions?
Many thanks
I am writing an application in which the position of an on screen object is controlled by one finger, and the position of another object is controlled by another finger. Wherever the two fingers move (not necessarily together) the objects follow. This is my code
Code:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count]==1) {
UITouch *touch = [touches anyObject];
firstTouch =[touch locationInView:self];
[self setNeedsDisplay];
///check to see which object has been selected and update its position
if (sqrt(pow(lhx-firstTouch.x,2)+pow(lhy-thumbL-(480-firstTouch.y),2))<50) {lhx=firstTouch.x; lhy=480-firstTouch.y+thumbL;}
else if (sqrt(pow(rhx-firstTouch.x,2)+pow(rhy-thumbR-(480-firstTouch.y),2))<50) {rhx=firstTouch.x; rhy=480-firstTouch.y+thumbL;}
}
if ([touches count]==2) {
NSArray *twoTouches = [touches allObjects];
UITouch *first =[twoTouches objectAtIndex:0];
firstTouch=[first locationInView:self];
UITouch *second =[twoTouches objectAtIndex:1];
secondTouch=[second locationInView:self];
//check to see which touch relates to which object and update positions
if (sqrt(pow(lhx-firstTouch.x,2)+pow(lhy-thumbL-(480-firstTouch.y),2))<50) {lhx=firstTouch.x; lhy=480-firstTouch.y+thumbL;}
else if (sqrt(pow(rhx-firstTouch.x,2)+pow(rhy-thumbR-(480-firstTouch.y),2))<50) {rhx=firstTouch.x; rhy=480-firstTouch.y+thumbL;}
else if (sqrt(pow(lhx-secondTouch.x,2)+pow(lhy-thumbL-(480-secondTouch.y),2))<50) {lhx=secondTouch.x; lhy=480-secondTouch.y+thumbL;}
else if (sqrt(pow(rhx-secondTouch.x,2)+pow(rhy-thumbR-(480-secondTouch.y),2))<50) {rhx=secondTouch.x; rhy=480-secondTouch.y+thumbL;}
}
}
However, this does not work well when both fingers are moving at the same time. If I move the two objects very slowly then there are no problems. But if I moved them any quicker then one object might not follow a finger, or the motion of an object, or both objects, might not be smooth etc.
Any suggestions?
Many thanks