我正在使用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语句是否正确?谢谢!
答案 0 :(得分: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;
}