How to get all UIViews at my touch position?
In image below I need to get all UIViews in touches with the yellow line.
Snippet from UI hierarchy:
I looking for a similar function to SpriteKit where we use to get all nodes at point with
self.nodes(at: touch.location(in: self))
Basic Swift Code for iOS AppsStep 2 − Open Main. storyboard and add one label as shown below. On this label we will show the touch position. Step 3 − Create @IBOutlet for the label, name it touchPositionLabel.
If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.
UITransitionView: Another private Apple class, this view is constrained by the LayoutContainerView and handles transitions between views by adding their wrapper. UIViewControllerWrapperView: This view does what it sounds like, even if the purpose of it doesn't seem obvious.
Views are the fundamental building blocks of your app's user interface, and the UIView class defines the behaviors that are common to all views. A view object renders content within its bounds rectangle, and handles any interactions with that content.
You have to traverse all views in the hierachy like:
extension UIView {
func allViews(for touch: UITouch) -> [UIView] {
return self.allViews(at: touch.location(in: self))
}
func allViews(at point: CGPoint) -> [UIView] {
var stack = [UIView]()
var result = [UIView]()
stack.append(self)
while let view = stack.popLast() {
let localPoint = view.convert(point, from: self)
if view.bounds.contains(localPoint) {
result.append(view)
}
stack.append(contentsOf: view.subviews)
}
return result
}
}
You can either start with any super view which contains all appropriate views (e.g the view of a view controller) or the window. The point for the second method must be relative to the bounds of the view which is represented by self
.
This can be achieved in two steps:
get all views starting from a certain view
func descendants(of view: UIView) -> [UIView] {
return view.subviews + view.subviews.flatMap(descendants(of:))
}
filter the view that contain the touch point:
let touchLocation = touchGesture.location(in: nil)
let matchingViews = descendants(of: UIApplication.shared.keyWindow!)
.filter { $0.convert($0.bounds, to: nil).contains(touchLocation) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With