2010-02-10 57 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'连接。

相关问题