Подскажите, почему Не срабатывает interesects при столкновении двух ImageView

import UIKit

class GameViewController: UIViewController {
    let minY: CGFloat = 400
    let maxY: CGFloat = 750
    

    @IBOutlet var backGround: UIImageView!
    @IBOutlet var boatTwo: UIImageView!
    @IBOutlet var boatOne: UIImageView!
    @IBOutlet var sharkOne: UIImageView!
    @IBOutlet var sharkTwo: UIImageView!
    @IBOutlet var submarine: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let submarineImage = UIImage(named: "submarine")
        self.submarine.image = submarineImage
        let boatOneImage = UIImage(named: "boat1")
        self.boatOne.image = boatOneImage
        let boatTwoImage = UIImage(named: "boat2")
        self.boatTwo.image = boatTwoImage
        let sharkOneImage = UIImage(named: "shark1")
        self.sharkOne.image = sharkOneImage
        let sharkTwoImage = UIImage(named: "shark2")
        self.sharkTwo.image = sharkTwoImage
        let backGoundImage = UIImage(named: "background")
        self.backGround.image = backGoundImage
        
        animateSharkOne()
        animateSharkTwo()
        animateBoatOne()
        animateBoatTwo()

    }
    
    @IBAction func upButtonPressed(_ sender: Any) {
        if submarine.frame.origin.y > minY {
            UIView.animate(withDuration: 0.3) {
                self.submarine.frame.origin.y -= 20
            }
        }
    }
    
        @IBAction func downButtonPressed(_ sender: Any) {
            if submarine.frame.origin.y < maxY {
                UIView.animate(withDuration: 0.3) {
                    self.submarine.frame.origin.y += 20
                }
            }
        }
    
func animateSharkOne() {
let animationDuration: TimeInterval = 5.0
sharkOne.frame.origin = CGPoint(x: 400, y: 500)
    UIView.animate(withDuration: animationDuration) {
        self.sharkOne.frame.origin.x -= 600
    }completion: { _ in
        self.animateSharkOne()
    }
    }
    
func animateSharkTwo() {
let animationDuration: TimeInterval = 3.5
sharkTwo.frame.origin = CGPoint(x: 400, y: 670)
UIView.animate(withDuration: animationDuration) {
self.sharkTwo.frame.origin.x -= 600
        } completion: { _ in
self.animateSharkTwo()
}
}
    
    func animateBoatOne() {
        let animationDuration: TimeInterval = 7.0
        boatOne.frame.origin = CGPoint(x: 400, y: 400)
        print("Boat One Frame: \(boatOne.frame)")
         print("Submarine Frame: \(submarine.frame)")
        UIView.animate(withDuration: animationDuration) {
            self.boatOne.frame.origin.x -= 600
        }completion: { _ in
            if self.submarine.frame.intersects(self.boatOne.frame) {
                self.endGame()
            } else {
                self.animateBoatOne()
            }
        }
        }
    func animateBoatTwo() {
        let animationDuration: TimeInterval = 5.0
        boatTwo.frame.origin = CGPoint(x: 450, y: 400)
        UIView.animate(withDuration: animationDuration) {
            self.boatTwo.frame.origin.x -= 600
        }completion: { _ in
            if self.submarine.frame.intersects(self.boatTwo.frame) {
                self.endGame()
            } else {
                self.animateBoatTwo()
            }
        }
        }
    func endGame() {
        print("Game Over")
        let controller = self.storyboard?.instantiateViewController(withIdentifier: "RestartViewController") as! RestartViewController
        self.navigationController?.pushViewController(controller, animated: true)
        print("End Game function executed")
    }
    }[![Скрин storyboadr'a][1]][1]


  [1]: https://i.stack.imgur.com/fbufe.jpg

Ответы (1 шт):

Автор решения: Oleg Soloviev

Потому что проверка на intersects у вас происходит в конце анимации, когда image уже не пересекаются. Вы можете использовать layer.presentation(), чтобы получить текущее положение image в течение анимации. Например, если вы поместите проверку в action нажатия кнопок, то вы сможете поймать intersects:

@IBAction func upButtonPressed(_ sender: Any) {
    if let submarinePresentation = submarine.layer.presentation(), let boatOnePresentation = boatOne.layer.presentation(), let boatTwoPresentation = boatTwo.layer.presentation(),
       (submarinePresentation.frame.intersects(boatOnePresentation.frame) || submarinePresentation.frame.intersects(boatTwoPresentation.frame)) {
        endGame()
    }
    if submarine.frame.origin.y > minY {
        UIView.animate(withDuration: 0.3) {
            self.submarine.frame.origin.y -= 20
        }
    }
}

@IBAction func downButtonPressed(_ sender: Any) {
    if let submarinePresentation = submarine.layer.presentation(), let boatOnePresentation = boatOne.layer.presentation(), let boatTwoPresentation = boatTwo.layer.presentation(),
       (submarinePresentation.frame.intersects(boatOnePresentation.frame) || submarinePresentation.frame.intersects(boatTwoPresentation.frame)) {
        endGame()
    }
    if submarine.frame.origin.y < maxY {
        UIView.animate(withDuration: 0.3) {
            self.submarine.frame.origin.y += 20
        }
    }
}
→ Ссылка