Tôi có quan điểm như sau (mà là một subview của bộ điều khiển xem tổng thể):(Swift) UIPanGestureRecognizer hoạt động tốt trên quan điểm cho đến khi subview được thêm vào nó
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
}()
và đây là nơi mà tôi đặt những hạn chế của tôi cho nó:
// 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)
}
đây là subview tôi thêm vào được bên trong của SuperView (mà tôi đang điều trị như một cái nhìn container):
lazy var subview: UIImageView = {
let sv = UIImageView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.contentMode = .scaleAspectFill
return sv
}()
Và những hạn chế của nó:
// 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
}
Cuối cùng, đây là nơi tôi thiết lập các chức năng cho các cử chỉ chảo:
// 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
}
}
}
Trước khi tôi thêm subview (và khi tôi nhận xét nó ra) các công trình cử chỉ chảo hoàn toàn tốt trên superview. Tuy nhiên, khi tôi hạn chế subview ở trung tâm của superview và thử di chuyển superview xung quanh View Controller, nó hoạt động kỳ quái (chỉ di chuyển sang trái một cách chính xác, liên tục giật trở lại trung tâm khi di chuyển sang phải).
Bất kỳ và tất cả trợ giúp đều được đánh giá cao.
Thử thêm panGesture vào superView bên dưới 'superView.addSubview (subView)' function –
Bạn có nghĩa là khi bạn thêm subview, cử chỉ pan của bạn ngừng hoạt động? – Lion
@Lion Chính xác, khi tôi thêm subView vào superView, cử chỉ pan sẽ ngừng hoạt động chính xác. – evanhaus