旋转框接触 uiview 时打印

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

在我的快速代码中,当球接触边框时,它应该打印 hit 。现在,球旋转,但当它接触 borderBox 时,没有打印任何内容,我尝试做一些事情,但它不起作用,也没有打印任何内容。我现在不知道如何解决这个问题。有没有人有什么想法。我添加了照片enter image description here

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;
    }
}
swift rotation position intersection overlap
1个回答
0
投票

正如您在评论中所要求的,这是用于检查球是否与做出假设的边界框碰撞的代码:

  1. 球实际上是一个圆
  2. 边界框实际上是包围圆的(意味着球在边界框内)
  3. 边界框具有固定的宽度和高度
func isBallWithinBoundingBox(ballPosition: CGPoint, ballRadius: Double, boundingBoxPosition: CGPoint, boundingBoxSize: CGSize) -> Bool { 
    let origin = boundingBoxPosition
    let xRelativeToBoundingBox = ballPosition.x - origin.x
    let yRelativeToBoundingBox = ballPosition.y - origin.y
    let xRange = 0 + ballRadius ... boundingBoxSize.width - ballRadius
    let yRange = 0 + ballRadius ... boundingBoxSize.height - ballRadius

    return xRange ~= xRelativeToBoundingBox && yRange ~= yRelativeToBoundingBox
}

您可以使用以下方法检查球是否在外面:

if !isBallWithinBoundingBox(ballPosition: <#ballPosition#>, ballRadius: <#ballRadius#>, boundingBoxPosition: <#boundingBoxPosition#>, boundingBoxSize: <#boundingBoxSize#>) { 
    // ball is outside of bounding box...
}

如果这个答案不能满足您的要求,请告诉我,我会尽力更好地解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.