首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift中UIPanGestureRecognizer的奇怪问题

Swift中UIPanGestureRecognizer的奇怪问题
EN

Stack Overflow用户
提问于 2018-09-22 19:26:31
回答 1查看 109关注 0票数 0

我试图在我的观点中添加潘手势,但我不知道为什么它不想工作。下面是我的UIView的代码:

代码语言:javascript
复制
    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中将它定义为:

代码语言:javascript
复制
var infoView: UIView!

我的pan手势方法看起来是:

代码语言:javascript
复制
@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)
}

我真的看不到我的错误。似乎一切都是正确的,但我似乎错了。你能指出我的错误吗?

更新

我的制约因素:

代码语言:javascript
复制
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]

哪里

代码语言:javascript
复制
let infoViewHeightConstraint = infoView.heightAnchor.constraint(greaterThanOrEqualToConstant: 112.0)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-23 01:39:49

我怀疑问题在于您的观点是GMSMapView的一个子视图。地图视图已经添加了一组手势识别器,手势识别器回调被发送到GMSMapView实例,而不是您的子视图。尽管在settings上有一个GMSMapView属性,但它允许您为映射配置用户界面设置。尝试下面的代码来启用手势:

代码语言:javascript
复制
self.mapView.settings.consumesGesturesInView = false

还有其他设置,您可以自定义那里。希望这能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52460131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档