2010-11-24 33 views
0

J'essaie de comprendre comment je peux définir un bouton pour ne pas être mis en surbrillance lorsque touché touches quitte le cadre du bouton sélectionné en cours. Voir le code ci-dessous. Le code ci-dessous devrait permettre à l'un des trois boutons d'être pressé individuellement et swipped. Le problème que je rencontre est que lorsque les boutons ont été déplacés, par exemple en utilisant les touches, les boutons appellent les actions, mais ne remettent jamais les boutons en surbrillance lorsque le balayage quitte le cadre du bouton.touchesmoved quitter frame

Merci à l'avance,

Azza

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event { 
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if((CGRectContainsPoint(Button1.frame, location)) && (!Button1.isHighlighted)) 
{ 
    if (!Button1.isHighlighted){ 
     [Button1 setHighlighted:YES]; 
     [self doAction1]; 
    }else{ 
     [Button1 setHighlight:NO]; 
    } 
} 

if((CGRectContainsPoint(Button2.frame, location)) && (!Button2.isHighlighted)) 
{ 
    if (!Button2.isHighlighted){ 
     [Button2 setHighlighted:YES]; 
     [self doAction2]; 
    }else{ 
     [Button2 setHighlight:NO]; 
    } 
} 

if((CGRectContainsPoint(Button3.frame, location)) && (!Button3.isHighlighted)) 
{ 
    if (!Button3.isHighlighted){ 
     [Button3 setHighlighted:YES]; 
     [self doAction3]; 
    }else{ 
     [Button3 setHighlight:NO]; 
    } 
} 
} 

- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event { 
for(UITouch *t in touches) { 

CGPoint location = [t locationInView:t.view]; 

if(CGRectContainsPoint(Button1.frame, location)) 
{ 
    if (!Button1.isHighlighted){ 
     [Button1 setHighlighted:YES]; 
     [self doAction1]; 
    } 
} 

if(CGRectContainsPoint(Button2.frame, location)) 
{ 
    if (!Button2.isHighlighted){ 
     [Button2 setHighlighted:YES]; 
     [self doAction2]; 
    } 
} 

if(CGRectContainsPoint(Button3.frame, location)) 
{ 
    if (!Button3.isHighlighted){ 
     [Button3 setHighlighted:YES]; 
     [self doAction3]; 
    } 
} 
} 


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

    for (UITouch *t in touches){ 
    CGPoint location = [t locationInView:self.view]; 

    if(CGRectContainsPoint(Button1.frame, location)) {  
     [Button1 setHighlighted:NO]; 

    } else if(CGRectContainsPoint(Button2.frame, location)) { 
      [Button2 setHighlighted:NO]; 

    } else if(CGRectContainsPoint(Button3.frame, location)) { 
     [Button3 setHighlighted:NO]; 
    } 
    } 
} 

Répondre

0

OK, je sais que je l'ai traité ce même problème auparavant. Je vois aussi une référence à cela dans mes propres commentaires dans le code. J'ai joint un morceau de code très ancien que j'ai écrit pour gérer des boutons simulés (similaire à ce que vous faites). Peut-être que regarder à travers cela aidera:

typedef struct { 
      BOOL enabled; 
      BOOL isActivated; 
      NSTimeInterval activatedStartTime; 
      NSTimeInterval holdTime; 
      CGRect frame; 
      UIImageView *glowImage; 
      BOOL processPending; 
      SEL callWhenTouched; 
      } SIM_BUTTON; 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    int fingers = [touches count]; 
    UITouch *touch; 
    CGPoint touchPoint; 
    int i, j; 

    for (i=0; i<fingers; i++) 
     { 
     touch = [[touches allObjects] objectAtIndex:i]; 
     touchPoint = [touch locationInView:self.view]; 


     for (j=0; j<SIM_BUTTON_COUNT; j++) 
      { 
      if (simButton[j].enabled && CGRectContainsPoint(simButton[j].frame, touchPoint)) 
       { 
       simButton[j].isActivated=YES; 
       simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate]; 
       simButton[j].processPending = YES; 
       } 
      } 
     } 
    } 



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    int fingers = [touches count]; 
    UITouch *touch; 
    CGPoint touchPoint; 
    CGPoint previousTouchPoint; 
    int i, j; 

    for (i=0; i<fingers; i++) 
     { 
     touch = [[touches allObjects] objectAtIndex:i]; 
     touchPoint = [touch locationInView:self.view]; 
     previousTouchPoint = [touch previousLocationInView:self.view]; 

     for (j=0; j<SIM_BUTTON_COUNT; j++) 
      { 
      if (simButton[j].enabled && simButton[j].isActivated && CGRectContainsPoint(simButton[j].frame, touchPoint) && CGRectContainsPoint(simButton[j].frame, previousTouchPoint)) 
       { 
       simButton[j].isActivated=YES; 
       } 
      else 
      if (simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint(simButton[j].frame, touchPoint) && CGRectContainsPoint(simButton[j].frame, previousTouchPoint)) 
       { 
       simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate]; 
       simButton[j].isActivated=YES; 
       } 
      else 
      if (simButton[j].enabled && simButton[j].isActivated && !CGRectContainsPoint(simButton[j].frame, touchPoint) && CGRectContainsPoint(simButton[j].frame, previousTouchPoint)) 
       { 
       simButton[j].isActivated=NO; 
       simButton[j].activatedStartTime = (NSTimeInterval)0.0; 
       } 
      else 
      if (simButton[j].enabled && !simButton[j].isActivated && CGRectContainsPoint(simButton[j].frame, touchPoint) && !CGRectContainsPoint(simButton[j].frame, previousTouchPoint)) 
       { 
       simButton[j].isActivated=YES; 
       simButton[j].activatedStartTime = [NSDate timeIntervalSinceReferenceDate]; 
       simButton[j].processPending = YES; 
       } 

      // 
      // If the user touched the SCAN button and then slid their finger off of the SCAN button 
      // 
      if (scannerActive==YES && simButton[RIGHT_SCAN_BUTTON].isActivated==NO && CGRectContainsPoint(simButton[RIGHT_SCAN_BUTTON].frame, previousTouchPoint)) 
       { 
       phraseMode = EXTERNAL_SCAN_PHRASES;  // Default 
       } 
      } 
     } 
    } 



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    int fingers = [touches count]; 
    UITouch *touch; 
    CGPoint touchPoint; 
    CGPoint previousTouchPoint; 
    int i, j; 

    for (i=0; i<fingers; i++) 
     { 
     touch = [[touches allObjects] objectAtIndex:i]; 
     touchPoint = [touch locationInView:self.view]; 
     previousTouchPoint = [touch previousLocationInView:self.view]; 

     for (j=0; j<SIM_BUTTON_COUNT; j++) 
      { 
      if (simButton[j].enabled && simButton[j].isActivated && (CGRectContainsPoint(simButton[j].frame, touchPoint) || CGRectContainsPoint(simButton[j].frame, previousTouchPoint))) 
       { 
       simButton[j].isActivated=NO; 
       simButton[j].activatedStartTime = (NSTimeInterval)0.0; 
       } 
      } 
     } 
    } 



- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    [self touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event]; 
    } 
+0

Cela a eu le swipping fonctionne très bien. Mais maintenant, quand je balaye un bouton et relâche entre les deux le premier bouton initial reste en surbrillance. le code ia dded est ci-dessous if ((! CGRectContainsPoint (Button1.frame, location)) && if (CGRectContainsPoint (Button1.frame, previouslocation))) {[button1 setHighlighted: NO];} J'ai ajouté un de ces boutons pour chaque bouton –

+0

OK , J'ai mis à jour ma réponse avec un morceau de vieux code où je sais que je traitais exactement ce même problème. Probablement l'écrire comme un objet au lieu d'une structure serait l'approche que j'utiliserais maintenant –

+0

Merci, je l'essayer quand je rentre à la maison et vous faire savoir comment je vais –