Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 看看是否没有接触过?_Ios_Objective C - Fatal编程技术网

Ios 看看是否没有接触过?

Ios 看看是否没有接触过?,ios,objective-c,Ios,Objective C,我知道使用iOS检测触摸是可能的 UITouch *touch = [[event allTouches] anyObject]; 但是,是否可以确定用户何时不触摸 编辑 我希望在用户5秒钟内不触摸屏幕时执行一个方法。这可能吗 我没有任何对触摸做出反应的自定义方法。我只有现有的方法 -touchesBegan -touchesMoved and -touchesEnded 更具体地说,用户可以随时触摸屏幕,不管他想触摸多久。但是,当用户触摸屏幕的时间超过5秒时,需要启动一个方法-sample

我知道使用iOS检测触摸是可能的

UITouch *touch = [[event allTouches] anyObject];
但是,是否可以确定用户何时不触摸

编辑

我希望在用户5秒钟内不触摸屏幕时执行一个方法。这可能吗

我没有任何对触摸做出反应的自定义方法。我只有现有的方法

-touchesBegan
-touchesMoved and
-touchesEnded

更具体地说,用户可以随时触摸屏幕,不管他想触摸多久。但是,当用户触摸屏幕的时间超过5秒时,需要启动一个方法
-sampleMethod

当然,其余时间。什么意思?唯一的方法是将布尔标志设置为false,在“touch”方法中,将其设置为true。然后,只要它是假的,就没有接触…

当然,剩下的时间。什么意思?唯一的方法是将布尔标志设置为false,在“touch”方法中,将其设置为true。那么,只要它是假的,就没有联系……

我会在这里对答案进行重击。因为在评论中你澄清了你想做什么。5秒钟后没有反应的东西。我在这里展示的内容通常用于opengl应用程序,我的所有应用程序都是。但是,即使您不在OpenGL中,类似的东西也应该适用于您

你需要一些持续运行的东西

    - (void) startAnimation
{
    if (!animating)
    {
        displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
        [displayLink setFrameInterval:animationFrameInterval];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        animating = TRUE;
    }
}

- (void)stopAnimation
{
    if (animating)
    {
        [displayLink invalidate];
        displayLink = nil;

        animating = FALSE;
    }
}
我们在oepngl应用程序中使用此选项,以每60秒运行一次drawview函数,并刷新显示。我不明白你为什么做不到。然后在drawView方法中,检查开始时的时间,并处理任何其他需要处理的垃圾,比如在游戏中推进棋子,或者只是检查消息出现的时间

- (void)drawView
{
timeThisRound = CFAbsoluteTimeGetCurrent();
并对照触发5秒开始的任何事件进行检查。如果你已经过了5秒,那么做你想做的事情,而不是再等他们点击按钮


我有自己的消息传递系统来实现这一点。我可以设置出现的任何消息,如果它在5秒钟后自行消失。或者,如果他们点击它,它就会消失得更快。我在任何地方都使用timeThisRound方法(一个全局属性)来跟踪“现在是什么时候”,这样我就可以拥有基于时间的东西以及基于触摸的东西。

我将在这里对答案进行一次重击。因为在评论中你澄清了你想做什么。5秒钟后没有反应的东西。我在这里展示的内容通常用于opengl应用程序,我的所有应用程序都是。但是,即使您不在OpenGL中,类似的东西也应该适用于您

你需要一些持续运行的东西

    - (void) startAnimation
{
    if (!animating)
    {
        displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
        [displayLink setFrameInterval:animationFrameInterval];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        animating = TRUE;
    }
}

- (void)stopAnimation
{
    if (animating)
    {
        [displayLink invalidate];
        displayLink = nil;

        animating = FALSE;
    }
}
我们在oepngl应用程序中使用此选项,以每60秒运行一次drawview函数,并刷新显示。我不明白你为什么做不到。然后在drawView方法中,检查开始时的时间,并处理任何其他需要处理的垃圾,比如在游戏中推进棋子,或者只是检查消息出现的时间

- (void)drawView
{
timeThisRound = CFAbsoluteTimeGetCurrent();
并对照触发5秒开始的任何事件进行检查。如果你已经过了5秒,那么做你想做的事情,而不是再等他们点击按钮


我有自己的消息传递系统来实现这一点。我可以设置出现的任何消息,如果它在5秒钟后自行消失。或者,如果他们点击它,它就会消失得更快。我在任何地方都使用timeThisRound方法(一个全局属性)来跟踪“现在是什么时候”,这样我就可以拥有基于计时的东西和基于触摸的东西。

您可以以5秒的间隔启动计时器,每次触摸时,重新启动计时器:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(yourMethod) userInfo:nil repeats:NO];
}

- (void)yourMethod {
    NSLog(@"not touched for 5 seconds");
}

根据您的具体需要,您可能希望改用
touchesend:withEvent

您可以以5秒的间隔启动计时器,每次触摸时,重新启动计时器:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(yourMethod) userInfo:nil repeats:NO];
}

- (void)yourMethod {
    NSLog(@"not touched for 5 seconds");
}

根据您的具体需要,您可能希望改用
touchesend:withEvent

在用户停止触摸视图后启动一个方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(sampleMethod) withObject:nil afterDelay:5.0f];
}
如果用户再次触摸视图,则应取消挂起的方法调用

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sampleMethod) object:nil];
}
记住在dealloc中也放一个挂起方法调用的cancel

- (void)dealloc {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sampleMethod) object:nil];
}

在一定延迟后启动一个方法,当用户停止触摸视图时启动

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(sampleMethod) withObject:nil afterDelay:5.0f];
}
如果用户再次触摸视图,则应取消挂起的方法调用

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sampleMethod) object:nil];
}
记住在dealloc中也放一个挂起方法调用的cancel

- (void)dealloc {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sampleMethod) object:nil];
}

如果事件未激活,则不会触动它。在活动中安排一位听众。你所谈论的世界有什么背景吗?我尽了全力才没有否决你的问题。我写这篇文章的时候有人写了。通常,计算机程序响应输入,在ios上触摸就是输入。你想让一些事情一直发生,当用户触摸和不触摸的时候会有所不同吗?用更多信息重写你的问题。@badweasel,我实际上想在用户5秒钟内不触摸时做点什么。现在更有意义了?那就把它放在你的问题里,我来回答。我们不知道你剩下的代码是什么样子的,如果你有任何定期调用的方法,比如displayLink类型的循环。。。像是一个拉维尤。除非你有一个经常被调用的方法,否则如果你的代码只是对触摸的反应,那就没有办法了。我们需要更多信息。如果事件未激活,则不会被触动。在活动中安排一位听众。你所谈论的世界有什么背景吗?我尽了全力才没有否决你的问题。我写这篇文章的时候有人写了。通常,计算机程序响应输入,在ios上触摸就是输入。你想让一些事情一直发生,当用户触摸和不触摸的时候会有所不同吗?用更多信息重写你的问题。@badweasel,我实际上想在用户5秒钟内不触摸时做点什么。更有意义