检查bool在x秒内是否为真 - Swift

问题描述 投票:0回答:1

我正在使用VNDetectRectanglesRequest,我想检查我检测到的矩形的CGRect是否在另一个CGRect中1秒钟。到目前为止,这是我的代码:

let x = self.view.frame.width * detectedRectangle.boundingBox.origin.x
let height = self.view.frame.height * detectedRectangle.boundingBox.height
let y = self.view.frame.height * (1 - detectedRectangle.boundingBox.origin.y) - height
let width = self.view.frame.width * detectedRectangle.boundingBox.width
let rectangleDetected = CGRect(x: x, y: y, width: width, height: height)

self.redView.frame = rectangleDetected

let outsideGuideRect = CGRect(x: self.boardMargin*1/2, y: (self.view.frame.height-self.view.frame.width+1/2*self.boardMargin)/2, width: self.view.frame.width-1/2*self.boardMargin, height: self.view.frame.width-1/2*self.boardMargin)
print(outsideGuideRect.contains(rectangleDetected))
if outsideGuideRect.contains(sudokuBoardRect) {

    // HERE I WANT TO CHECK IF THIS IF STATEMENT IS TRUE FOR 1 second

}

上面的代码是在每帧运行的函数中。我怎么能检查if语句是否为真一秒?谢谢!

ios swift avcapturesession vision
1个回答
1
投票

我的理解形式是你的问题是,“你想在你的直肠B进入直肠A(不离开)后1秒后执行一个动作”。

以下可能是我从头脑中得到的一个可能的解决方案,如果我们想的话,可以有更好的解决方案。

//initialize _insideRect as false globally
if outsideGuideRect.contains(sudokuBoardRect) {
    // HERE I WANT TO CHECK IF THIS IF STATEMENT IS TRUE FOR 1 second
    if (!_insideRect) {
      _insideRect = true;
      DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(0.1)) {
        if (_insideRect) {
          // your function here
        }
      }
    }
}else{
    _insideRect = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.