我使用- (void) touchesMoved做的东西当过I 输入一个特定的帧,在此情况下的按钮的区域中。

我的问题是,我只希望它做的东西,当我进入框架 - 不可以当我移动我的手指在框架内。

有谁知道我怎么能叫我的方法只有一次,而我是在框架内,仍然允许我再次呼吁,如果我在同一个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];
    }
}
有帮助吗?

解决方案

您可以使用一些属性来检查,如果当你进入特定区域的代码已经被调用。它看起来像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];
}

其他提示

Simply add a BOOL您在touchesMoved检查并在touchesEnded重置

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

尝试使用一个UIButton并使用“触摸拖拽输入”在界面生成器连接。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top