I'm implementing touchesMoved, touchesBegan, and touchesEnded on a few UIButtons, so that I can slide my fingers over them and have them call the appropriate actions.
It seems to be working almost as intended, however, if I press two fingers outside of the two buttons' frames, and then slide them into the buttons' frames at the same time, the function within touchesMoved gets called multiple times. Instead, it should only call each button's function once while in the button's frame.
Below is my code.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
if (!Button1.isHighlighted){
if(!button1Highlighted) {
[self doAction1];
}
[Button1 setHighlighted:YES];
button1Highlighted = YES;
}
}
else {
[Button1 setHighlighted:NO];
button1Highlighted = NO;
}
if(CGRectContainsPoint(Button2.frame, location))
{
if (!Button2.isHighlighted){
if(!button2Highlighted) {
[self doAction2];
}
[Button2 setHighlighted:YES];
button2Highlighted = YES;
}
}
else {
[Button2 setHighlighted:NO];
button2Highlighted = NO;
}
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
[Button1 setHighlighted:YES];
button1Highlighted = YES;
[self doAction1];
}
if(CGRectContainsPoint(Button2.frame, location))
{
[Button2 setHighlighted:YES];
button2Highlighted = YES;
[self doAction2];
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *t in touches) {
CGPoint location = [t locationInView:t.view];
if(CGRectContainsPoint(Button1.frame, location))
{
[Button1 setHighlighted:NO];
button1Highlighted = NO;
}
if(CGRectContainsPoint(Button2.frame, location))
{
[Button2 setHighlighted:NO];
Button2Highlighted = NO;
}
}
}
Any help is greatly appeciated. Thanks!