Help with touchesMoved when dragging fingers
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!
If -touchesMoved: is getting called with multiple touches such that there's one touch on Button1 and another on Button2 and both buttons are not highlighted, then the touch over Button1 will highlight Button1 and unhighlight Button2. Meanwhile, in the same loop in the same call to -touchesMoved:, the touch over Button2 will essentially reset Button1's highlight state back to unhighlighted.
-touchesMoved: will be called as long as there are touches, and each call will cycle the two buttons again.
Maybe you need to add a 'hasBeenHighlighted' property to your buttons. I am not sure how best to initialize this property to NO for all your buttons. But it would need to be set to YES inside -setHighlighted: and it would need to be checked before calling -doActionX.
I am not sure that I understand exactly what you are trying to achieve, but I hope this is of some help.
I ended up getting it to work by storing the number of touches on the screen in a variable called touchesCount. I then increment it in touchesBegan, and decrease it in touchesEnded. Then in the touchesMoved before calling doActionX, I checked to make sure touchesCount < 2.
精彩评论