I want to allow users to add a yellow square UIView to a UIScrollView and then both drag and drop and zoom the yellow squares that the user has created.
I understand that to enable this I need to create a secondary view, say yellowSquareContainerView that I then set up as follows:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return yellowSquareContainerView
}
I add the yellow squares to this container view.
However the main problem with this approach is that the UIPanGestureRecognizer added to the yellow UIView doesn't work if the user moves the square outside of the bounds of the yellowSquareContainerView (which is a completely valid use case).
How can I get the UIPanGestureRecognizer to work when the yellow squares are outside of the bounds of the container view?
After hacking around, I figured out an approach to panning a view outside the bounds of its parent view. It involves overriding the parent view's
pointfunction.Internally, a view uses its point function to determine if an event (like touch or pan) should be allowed. By default, point checks if the event starts in the view's bounds. That's why you can pan a view outside of its parent, but you can't pan it after that. By overriding point to always return true, the gesture is always allowed.
Here are the steps:
point(inside:with:)to always return trueHere is an example subclass:
And here is some example code to demonstrate: