(Swift) UIPanGestureRecognizer works fine while browsing until a subview is added to it

I have the following view (which is a subview of the general view controller):

lazy var superView: UIView = { let cv = UIView() cv.backgroundColor = .gray cv.translatesAutoresizingMaskIntoConstraints = false cv.layer.cornerRadius = 5 cv.layer.masksToBounds = true cv.isUserInteractionEnabled = true cv.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan))) return cv }() 

and this is where I set my limits for it:

 // constraints for super view func setupSuperView() { superView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true superView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true superView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true superView.heightAnchor.constraint(equalTo: view.heightAnchor, constant: -200).isActive = true // ISSUE: For some reason, adding this subview with or without constraints screws up the pan functionality of the superview superView.addSubview(subView) } 

this is the sub-item that I add to be inside the supervisor (which I see as representing the container):

 lazy var subview: UIImageView = { let sv = UIImageView() sv.translatesAutoresizingMaskIntoConstraints = false sv.contentMode = .scaleAspectFill return sv }() 

And its limitations:

 // constraints for yes/no view within superview func setupSubView() { subView.centerXAnchor.constraint(equalTo: superView.centerXAnchor).isActive = true subView.centerYAnchor.constraint(equalTo: superView.centerYAnchor).isActive = true } 

Finally, here I set the functionality for panorama gestures:

  // pan functionality for swiping on superview func handlePan(gesture: UIPanGestureRecognizer) { guard let superview = gesture.view else { return } // point you are tapped on let point = gesture.translation(in: view) // reference for how far left or right of the center of the superview your pan is let xFromCenter = superview.center.x - view.center.x // allows the superview to move with your finger superview.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y) // dragged right if xFromCenter > 0 { subView.image = #imageLiteral(resourceName: "yes") subView.tintColor = UIColor.green // else dragged left } else { subView.image = #imageLiteral(resourceName: "no") subView.tintColor = UIColor.red } // when you let go if gesture.state == UIGestureRecognizerState.ended { // animate superview back to center where it originally was UIView.animate(withDuration: 0.2) { superview.center = self.view.center } } } 

Before I added subview (and when I comment on it), the pan gesture works great on a supervisor. However, when I restrict the subview, being in the center of the supervisor, and try to move the supervisor around the view controller, it acts in a weird way (only moving to the right correctly, constantly twitching to the center when moving to the right).

Any help is appreciated.

+5
source share
2 answers

I understood the question. Although I don’t know the reason why this fix, the problem is that the image that I used for the β€œno” option to pan the view of the container to the left caused the entire view to behave completely because it is a different image.

I tested this by doing both yes, regardless of whether you pan them left or right. This fixed the problem that led me to think that the problem is switching the image depending on whether you are scrolling left or right. I don’t know why he has a problem with displaying two different images, but I will try to understand this too.

Thank you for your useful contribution!

0
source

disable userinteraction in your subquery and it will work, I think!

You code what should be

  subView.isUserInteractionEnabled = false 
+1
source

Source: https://habr.com/ru/post/1271557/


All Articles