如何在 -(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];
}
}
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.
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.
Thank you.
-(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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用某些属性来检查当您进入特定区域时是否已经调用了该代码。看起来
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:只需添加一个 BOOL
,您在 TouchesMoved 中检查它并在 TouchesEnded 中重置Simply add a BOOL
that you check in touchesMoved and reset in touchesEnded尝试使用 UIButton 并使用 Interface Builder 中的“触摸拖动输入”连接。
try using a UIButton and use the 'touch drag enter' connection in Interface Builder.