我正在尝试向视图添加平移手势,但我不知道为什么它不起作用。这是我的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)
答案 0 :(得分:1)
我怀疑问题在于您的视图是x = [1,2,3]
x[0] = 10
y = (1,2,3)
y[0] = 10 # this will raise an error. tuple is not mutable.
y = x
id(y) == id(x) #gives true. since y is a reference to x
y[0] = 10
print(y)
[10, 2, 3]
print(x)
[10, 2, 3] # x is changed as well! y and x are same same.
的子视图。地图视图已经添加了许多手势识别器,并且手势识别器回调已发送到GMSMapView
实例,而不是您的子视图。尽管GMSMapView
上有一个settings
属性,可让您配置地图的用户界面设置。尝试以下代码以启用手势:
GMSMapView
还有其他可以在此处自定义的设置。希望这会有所帮助