ios - Swift: hitTest for UIView underneath another UIView

Ios - Swift: hitTest for UIView underneath another UIView

In iOS development with Swift, when you want to determine which UIView is underneath another UIView at a specific point, you can use the hitTest(_:with:) method. This method is particularly useful when you have overlapping views and need to find out which one should handle a touch event, for example.

Here's how you can use hitTest(_:with:) to find the UIView underneath another UIView:

  1. Implement hitTest(_:with:) in the superview:

    Assume you have a superview (parentView) containing several subviews (viewA, viewB, etc.). If viewB is on top of viewA, and you want to find out if viewA is underneath viewB at a specific point, you can implement hitTest(_:with:) in parentView.

    let point = CGPoint(x: xCoord, y: yCoord) // Replace with your specific point // Assuming parentView is the superview containing viewA and viewB let viewUnderneath = parentView.hitTest(point, with: nil) if viewUnderneath == viewA { // viewA is underneath the point print("viewA is underneath viewB at point \(point)") } else { // viewB or another view is underneath the point print("viewA is not underneath viewB at point \(point)") } 
  2. Understanding the method:

    • hitTest(_:with:) recursively calls point(inside:with:) on the receiver and its subviews, returning the farthest descendant that contains the specified point.
    • point is the point in the coordinate system of parentView where you want to check for the presence of viewA or viewB.
    • nil as the second argument (with:) means that UIKit will handle the event targeting automatically and does not require any additional context.
  3. Considerations:

    • Ensure that the userInteractionEnabled property of the views (viewA and viewB) is set to true if you want them to respond to touch events.
    • This method works well for determining the frontmost visible view at a point, even if there are overlapping views.
    • If the views have transparent areas or if they overlap in complex ways, the result of hitTest(_:with:) may not be intuitive without considering the view hierarchy.

By using hitTest(_:with:), you can effectively determine which UIView is underneath another UIView at a specific point in your iOS app developed with Swift.

Examples

  1. Swift hitTest ignore top view Description: Perform hit testing on a UIView to find the view underneath it in Swift.

    // Example of hit testing to find view underneath in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint == topView { return nil // Ignore topView } return viewUnderPoint } 
  2. Swift hitTest ignore overlay view Description: Exclude a specific overlay UIView from hit testing in Swift.

    // Example of excluding overlay view from hit testing in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint == overlayView { return nil // Ignore overlayView } return viewUnderPoint } 
  3. Swift hitTest ignore transparent view Description: Skip hit testing on a transparent or partially transparent UIView in Swift.

    // Example of skipping hit testing on transparent view in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint?.alpha == 0 { return nil // Ignore transparent view } return viewUnderPoint } 
  4. Swift hitTest ignore hidden view Description: Exclude hidden UIViews from hit testing in Swift.

    // Example of excluding hidden view from hit testing in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if let view = viewUnderPoint, view.isHidden { return nil // Ignore hidden view } return viewUnderPoint } 
  5. Swift hitTest pass through view Description: Allow touch events to pass through a specific UIView in Swift.

    // Example of allowing touch events to pass through view in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint == passThroughView { return nil // Allow touch to pass through passThroughView } return viewUnderPoint } 
  6. Swift hitTest multiple views Description: Perform hit testing on multiple UIViews to find the topmost one in Swift.

    // Example of hit testing multiple views in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { var viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint == nil { viewUnderPoint = anotherView.hitTest(anotherView.convert(point, from: self), with: event) } return viewUnderPoint } 
  7. Swift hitTest custom behavior Description: Implement custom hit testing behavior for UIViews in Swift.

    // Example of custom hit testing behavior in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { var viewUnderPoint = super.hitTest(point, with: event) if let view = viewUnderPoint { if view.tag == 100 { // Custom handling for view with tag 100 } } return viewUnderPoint } 
  8. Swift hitTest subviews Description: Use hit testing to find a specific subview within a UIView hierarchy in Swift.

    // Example of hit testing for subview in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let viewUnderPoint = super.hitTest(point, with: event) if let subview = viewUnderPoint?.subviews.first(where: { $0.tag == 200 }) { // Found subview with tag 200 return subview } return viewUnderPoint } 
  9. Swift hitTest bounds Description: Limit hit testing within specified bounds of a UIView in Swift.

    // Example of hit testing within bounds in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let bounds = CGRect(x: 0, y: 0, width: 100, height: 100) guard bounds.contains(point) else { return nil // Point is outside bounds } return super.hitTest(point, with: event) } 
  10. Swift hitTest non-overlapping views Description: Handle hit testing when UIViews do not overlap perfectly in Swift.

    // Example of hit testing for non-overlapping views in Swift override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { var viewUnderPoint = super.hitTest(point, with: event) if viewUnderPoint == nil { let adjustedPoint = convert(point, to: anotherView) viewUnderPoint = anotherView.hitTest(adjustedPoint, with: event) } return viewUnderPoint } 

More Tags

android-layout edit uft14 python-2.7 html2canvas artifactory windows-runtime onscrolllistener tripledes teamcity

More Programming Questions

More Trees & Forestry Calculators

More Electrochemistry Calculators

More Fitness Calculators

More Geometry Calculators