我正试图添加潘手势我的看法,但我不知道为什么它不想工作。以下是我的UIView代码:
self.infoView = UIView()
self.mapView.addSubview(infoView)
infoView.backgroundColor = .white
infoView.layer.cornerRadius = 10.0
infoView.layer.masksToBounds = true
infoView.isUserInteractionEnabled = true
infoView.translatesAutoresizingMaskIntoConstraints = false
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.moveInfoView(recognizer:)))
infoView.addGestureRecognizer(panGesture)
我正在我的生活中定义它
UIViewController
作为:
var infoView: UIView!
我的平移手势方法如下所示:
@objc func moveInfoView(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)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
}
更新
我的限制:
let constraints = [
// InfoView constraints
NSLayoutConstraint(item: infoView, attribute: .bottom, relatedBy: .equal, toItem: self.view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: infoView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: infoView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: 0.0),
infoViewHeightConstraint]
哪里
let infoViewHeightConstraint = infoView.heightAnchor.constraint(greaterThanOrEqualToConstant: 112.0)