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 =
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.