Domanda multitouch
-
06-07-2019 - |
Domanda
Voglio rilevare quale parte dello schermo viene toccata quando l'utente scuote iPhone.
Lo faccio nel modo seguente:
-(void) accelerometer: (UIAccelerometer*)accelerometer didAccelerate: (UIAcceleration*)acceleration
{
float shakeStrength = sqrt( acceleration.x * acceleration.x + acceleration.y * acceleration.y + acceleration.z * acceleration.z );
if (shakeStrength >= 1.5f)
{
if (isLeftHandTouches && isRightHandTouches)
{
DebugLog(@"both hands shake");
} else if (isLeftHandTouches)
{
DebugLog(@"left hand shake");
} else if (isRightHandTouches)
{
DebugLog(@"right hand shake");
}
}
}
-(void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for (int i = 0; i < [allTouches count]; i++)
{
if ([ [ [allTouches allObjects] objectAtIndex: i] locationInView: [self view] ].x <= 240.0f)
{
isLeftHandTouches = YES;
} else
{
isRightHandTouches = YES;
}
}
}
-(void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
NSSet *allTouches = [event allTouches];
for (int i = 0; i < [allTouches count]; i++)
{
if ([ [ [allTouches allObjects] objectAtIndex: i] locationInView: [self view] ].x <= 240.0f)
{
isLeftHandTouches = NO;
} else
{
isRightHandTouches = NO;
}
}
}
Tutto funziona bene se l'utente rimuove entrambe le mani prima di fare un'altra vibrazione, ma tutto viene incasinato se ho entrambe le mani sullo schermo e rimuovo una di esse.
vale a dire. Stringo con entrambe le mani sullo schermo e successivamente voglio scuotere l'iPhone con una sola mano. In questo caso la vibrazione non conta - come se non ci fossero tocchi sullo schermo. Suppongo che quando rimuovo una mano dallo schermo, entrambi "toccano" vengono rimossi.
Qual è il problema e come posso risolverlo?
Grazie.
Soluzione
Perché stai enumerando -allTouches
? Basta enumerare il set tocchi
passato. Lo stesso vale per entrambi i metodi.