如何在-(void)touchesMoved期间仅调用一次方法? [英] How to call a method only once during -(void)touchesMoved?

查看:93
本文介绍了如何在-(void)touchesMoved期间仅调用一次方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我输入特定的帧(在这种情况下是按钮的区域)时,我都会使用- (void) touchesMoved进行操作.

I am using - (void) touchesMoved to do stuff when ever I enter a specific frame, in this case the area of a button.

我的问题是,我只希望它在进入框架时做一些事情-在我将手指移入框架中时.

My problem is, I only want it to do stuff when I enter the frame - not when I am moving my finger inside the frame.

有人知道我如何只能在框架内调用一次我的方法,并且如果我在同一touchMove中重新输入它,仍然允许我再次调用它.

Does anyone know how I can call my methods only once while I am inside the frame, and still allow me to call it once again if I re-enter it in the same 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对象的突出显示状态(不确定它是什么)可能适合于此:

You can use some attribute to check if the code was already called when you were entering specific area. It looks like highlighted state of p1 object (not sure what it is) may be appropriate for that:

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

这篇关于如何在-(void)touchesMoved期间仅调用一次方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆