Print when a rotating a box touches a uiview

In my swift code when ball touches border box it should print hit. Right now the ball rotates but nothihing is being printed when it touches borderBox I tried to do something but It is not working and nothing is being printed. I don’t now how to resolve this. Does anyone have any ideas.

import UIKit
class ViewController: UIViewController {
    var ball = UIImageView()
    
    var left = UIButton()

    var borderBox = UIView()

    var isRotating = true // Variable to control rotation
    var currentRotationAngle: CGFloat = 0.0
    var rotationAnimator: UIViewPropertyAnimator?
    
    var isTouchingBorderBox = false

    override func viewDidLoad() {
        super.viewDidLoad()
      
        [ball,left,borderBox ].forEach {
            view.addSubview($0)
            $0.translatesAutoresizingMaskIntoConstraints = false
            $0.layer.borderWidth = 1
            
        }
        
        
        ball.backgroundColor = .systemPink
        
        NSLayoutConstraint.activate([
            ball.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25),
            ball.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.25),
            ball.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            ball.bottomAnchor.constraint(equalTo: view.centerYAnchor),

            left.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            left.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10, constant: 0),
            left.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5, constant: 0),
            left.leadingAnchor.constraint(equalTo: view.leadingAnchor),

            borderBox.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1),
            borderBox.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
            borderBox.centerXAnchor.constraint(equalTo: view.centerXAnchor),
       borderBox.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -view.bounds.height * 0.3),

        ])

        borderBox.backgroundColor = .gray

     
        left.addTarget(self, action: #selector(leftM), for: .touchDown)
        
        
        
    }
    func handleTouchBorderBox() {
        if !isTouchingBorderBox {
            print("Ball touches BorderBox")
            isTouchingBorderBox = true
        }
    }


       func Rotate2() {
           rotationAnimator = UIViewPropertyAnimator(duration: 1, curve: .linear, animations: {
               if self.isRotating {
                   self.currentRotationAngle += CGFloat.pi / -1.5
                   self.ball.transform = self.ball.transform.rotated(by: CGFloat.pi / -1.5)
               }
           })

           rotationAnimator?.addCompletion { [weak self] _ in
               if self?.isRotating == true {
                   self?.Rotate2()
               }

               // Check if box and borderBox intersect and handle it
               if self?.ball.frame.intersects(self?.borderBox.frame ?? .zero) == true {
                   self?.handleTouchBorderBox()
               }
           }

           rotationAnimator?.startAnimation()
       }

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        view.setNeedsLayout()
        view.layoutIfNeeded()
        
        self.ball.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
        
 
    }
    
    
    @objc func leftM(){
        Rotate2()
    }

}

extension UIView{
    func setAnchorPoint(anchorPoint: CGPoint) {
        
        var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
        var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
        
        newPoint = newPoint.applying(self.transform)
        oldPoint = oldPoint.applying(self.transform)
        
        var position : CGPoint = self.layer.position
        
        position.x -= oldPoint.x
        position.x += newPoint.x;
        
        position.y -= oldPoint.y;
        position.y += newPoint.y;
        
        self.layer.position = position;
        self.layer.anchorPoint = anchorPoint;
    }
}

Hi @timswift ! I would try adding this code if ball.frame.intersects(borderBox.frame) { print("Ball touches BorderBox")} to the Rotate2( ) function. This line of code will check if the ball’s frame intersects with the border’s box’s frame. If it does, then the code will print “Ball touches BorderBox” to the console.

Overview

func Rotate2() {
    rotationAnimator = UIViewPropertyAnimator(duration: 1, curve: .linear, animations: {
        if self.isRotating {
            self.currentRotationAngle += CGFloat.pi / -1.5
            self.ball.transform = self.ball.transform.rotated(by: CGFloat.pi / -1.5)
        }
    })

    rotationAnimator?.addCompletion { [weak self] _ in
        if self?.isRotating == true {
            self?.Rotate2()
        }

        // Check if box and borderBox intersect and handle it
        if self?.ball.frame.intersects(self?.borderBox.frame ?? .zero) == true {
            print("Ball touches BorderBox")
        }
    }

    rotationAnimator?.startAnimation()
}

This topic was automatically closed after 166 days. New replies are no longer allowed.