2010-11-24 10 views
0

touchesmovedが現在選択されているボタンのフレームを離れるときに、どのボタンを強調表示しないように設定できるかを調べようとしています。以下のコードを参照してください。以下のコードでは、3つのボタンのいずれかを個別に押してスワイプすることができます。私が抱えている問題は、ボタンがスウィープされたとき、例えばtouchesmovedを使用してボタンがアクションを呼び出すときですが、スワイプがボタンフレームを離れるときにボタンが戻らないように戻すことは決してありません。事前にtouchesmoved leave frame

おかげで、

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]; 
    } 
    } 
} 

答えて

0

OK、私は前にこの同じ問題を扱ってきました知っています。私はまた、コード内の自分のコメントでそれへの参照を参照してください。私はシミュレートされたボタンを処理するために書いた非常に古いコードの塊を添付しました。多分これを見て助けてくれるでしょう:

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

しかし、今私はボタンをスワイプし、それらの両方の間で解放すると、最初の最初のボタンが強調表示されたままになります。コードia ddedは以下の通りですif(CGRectContainsPoint(Button1.frame、location))&& if(CGRectContainsPoint(Button1.frame、previouslocation))){[button1 setHighlighted:NO];}各ボタンにこれらのいずれかを追加しました –

+0

OK私はこのまったく同じ問題を扱っていたことを知っている古いコードの塊で私の答えを更新しました。おそらく構造体の代わりにオブジェクトとして書くと、今使っているアプローチになります –

+0

ありがとう、私は家に帰ると試してみると、どうすればいいのか教えてください –