How to: bring view to front on tap; set max/min pi

2019-05-27 03:28发布

NB: everything I found is too old or way too different from what I actually need, also I tried almost everything I found and nothing worked the right way.

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:

https://github.com/marybnq/Views

1条回答
Viruses.
2楼-- · 2019-05-27 03:47

I solved my problem!

  1. I figured out how to set a max/ min scale for the UIPinchGestureRecognizer, here's what I did.
    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
            }
        }
    }
  1. In order to bring views to the front I created multiple UIPanGestureRecognizers and I added bringSubviewToFront:
     @IBAction func handlePan1(recognizer:UIPanGestureRecognizer) {
        let translation = recognizer.translation(in: self.view)
        if let view = recognizer.view {
            view.center = CGPoint(x:view.center.x + translation.x,
                                  y:view.center.y + translation.y)
        }
        view.bringSubviewToFront(view1)
        recognizer.setTranslation(CGPoint.zero, in: self.view)
    }

     @IBAction func handlePan2(recognizer:UIPanGestureRecognizer) {
        let translation = recognizer.translation(in: self.view)
        if let view = recognizer.view {
            view.center = CGPoint(x:view.center.x + translation.x,
                                  y:view.center.y + translation.y)
        }
        view.bringSubviewToFront(view2)
        recognizer.setTranslation(CGPoint.zero, in: self.view)
    }

ecc... BUT you can't do this if you want to limit the pan gesture as well, so you might want to add a tap gestureRecognizer instead:

Add a gestureRecognizer to your view and set the delegate.

@IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
        view.bringSubviewToFront(myView)
    }

In your viewDidLoad:

 let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
  1. To set a screen limit/ pan limit, I found this code:
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
        let translation = recognizer.translation(in: self.view)
        let statusFrame = UIApplication.shared.statusBarFrame


        if let recognizerView = recognizer.view {
            if recognizerView.frame.origin.x < 0.0 {
                recognizerView.frame.origin = CGPoint(x: 0.0, y: recognizerView.frame.origin.y)
            }
            if recognizerView.frame.origin.y < statusFrame.height {
                recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: statusFrame.height)
            }
            if recognizerView.frame.origin.x + recognizerView.frame.size.width > view.frame.width {
                recognizerView.frame.origin = CGPoint(x: view.frame.width - recognizerView.frame.size.width, y: recognizerView.frame.origin.y)

            }
            if recognizerView.frame.origin.y + recognizerView.frame.size.height > view.frame.height {
                recognizerView.frame.origin = CGPoint(x: recognizerView.frame.origin.x, y: view.frame.height - recognizerView.frame.size.height)
            }
        }

        if let centerX = recognizer.view?.center.x, let centerY = recognizer.view?.center.y {
            recognizer.view?.center = CGPoint.init(x: centerX + translation.x , y: centerY + translation.y)
            recognizer.setTranslation(CGPoint.zero, in: self.view)
        }

        view.bringSubviewToFront(view1)
        recognizer.setTranslation(CGPoint.zero, in: self.view)
}
查看更多
登录 后发表回答