UIGestureRecognizer help

NB: everything I found is too old or way too different from what I actually need.

I created 3 UIViews, I can drag them around and zoom them.
Now, I need to:

  1. Bring some of them to the front: I want to add to each of them an
    UITapGesture that allows me to bring the tapped UIView to the front
    passing the other UIViews to the back;
  2. Set a max/ min scale for the UIPinchGestureRecognizer so that I can't have super large UIiews or tiny UIViews;
  3. Set a sort of screen limit to the UIPanGestureRecognizer so that I can't drag my UIViews outside of the main view.

Here's my code:

I’m not in front of my Mac right now, but here are some tips.

Try using this for zoom limits. It’s objc, but logic is what you need.

You just rearrange subviews to change depth. Bring the tapped view forward.

Make sure you add code to allow multiple gestures, there’s some delegate apis.

You can prevent movement outside a region by capping x/y coordinates in your pan gesture translate logic.

Hi, I already saw that answer but it didn't really work for me... I think my code already allows multiple gestures, but I don't really know how to do the rest as I just started learning... I'm sorry, could you please be more specific? Thanks a lot!

That's how I solved the max/min pinch:

var recognizerScale:CGFloat = 1.0
var maxScale:CGFloat = 1.5
var minScale:CGFloat = 0.8

@IBAction func handlePinch(_ pinchGesture: UIPinchGestureRecognizer) {
    guard pinchGesture.view != nil else {return}
    
    if pinchGesture.state == .began || pinchGesture.state == .changed{
        if (recognizerScale < maxScale && pinchGesture.scale > 1.0) {
            pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
            recognizerScale *= pinchGesture.scale
            pinchGesture.scale = 1.0
        }
        if (recognizerScale > minScale && pinchGesture.scale < 1.0) {
            pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
            recognizerScale *= pinchGesture.scale
            pinchGesture.scale = 1.0
        }
    }
}
Terms of Service

Privacy Policy

Cookie Policy