# Print when a rotating a box touches a uiview

**URL:** <https://forums.kodeco.com/t/print-when-a-rotating-a-box-touches-a-uiview/198487>\
**Category:** Swift\
**Tags:** ios\
**Created:** [October 25, 2023, 2:59am UTC](https://forums.kodeco.com/t/print-when-a-rotating-a-box-touches-a-uiview/198487 "2023-10-25T02:59:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [October 25, 2023, 2:59am UTC](https://forums.kodeco.com/t/print-when-a-rotating-a-box-touches-a-uiview/198487/1 "2023-10-25T02:59:05Z")

</div>

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.

```auto
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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![gdelarosa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gdelarosa/32/109063_2.png) [@gdelarosa](https://forums.kodeco.com/u/gdelarosa)\
**Post date:** [November 6, 2023, 4:32pm UTC](https://forums.kodeco.com/t/print-when-a-rotating-a-box-touches-a-uiview/198487/2 "2023-11-06T16:32:45Z")

</div>

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

```auto
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()
}

```

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 8, 2024, 6:59pm UTC](https://forums.kodeco.com/t/print-when-a-rotating-a-box-touches-a-uiview/198487/3 "2024-04-08T18:59:06Z")

</div>

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