2010-02-10 22 views
1

我使用- (void) touchesMoved做任何事情時,我輸入一個特定的框架,在這種情況下,按鈕的區域。如何在 - (void)touchesMoved期間只調用一次方法?

我的問題是,當我在框架中移動手指時,我只希望它在進入框架時執行某些操作 - 而不是

有沒有人知道我在框架內部時只能調用我的方法一次,如果我在同一個touchMove中重新輸入它,仍然允許我再次調用它。

謝謝。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event touchesForView:self.view] anyObject]; 

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

    if(CGRectContainsPoint(p1.frame, location)) 
    { 
      //I only want the below to me called 
      // once while I am inside this frame 
     [self pP01]; 
     [p1 setHighlighted:YES]; 
    }else { 
     [p1 setHighlighted:NO]; 
    } 
} 

回答

1

您可以使用一些屬性來檢查當您進入特定區域時是否已經調用了該代碼。它看起來像p1對象(這是不知道)的聚焦狀態可以針對合適:

if(CGRectContainsPoint(p1.frame, location)) 
{ 
    if (!p1.isHighlighted){ // We entered the area but have not run highlighting code yet 
     //I only want the below to me called 
     // once while I am inside this frame 
     [self pP01]; 
     [p1 setHighlighted:YES]; 
    } 
}else { // We left the area - so we'll call highlighting code when we enter next time 
    [p1 setHighlighted:NO]; 
} 
1

Simply add a BOOL您在touchesMoved檢查並重置touchesEnded

+0

在退出所需區域時,您還需要重置此bool標誌:請參閱「如果我再次在同一個touchMove中重新輸入它,請仍然允許我再次調用它」。在問題中的要求 – Vladimir 2010-02-10 09:07:18

+0

@Vladimir - 謝謝,我在那裏馬虎讀書 – willcodejavaforfood 2010-02-10 09:26:49

1
if(CGRectContainsPoint([p1 frame],[touch locationInView:self.view])) { 
    NSLog (@"Touch Moved over p1"); 
    if (!p14.isHighlighted) { 
    [self action: p1]; 
    p1.highlighted = YES; 
    } 
    }else { 
    p1.highlighted = NO; 
    } 
0

嘗試使用一個UIButton和在Interface Builder中使用'touch drag enter'連接。

相關問題