如何在UIPanGestureRecognizer上设置约束?

时间:2017-02-06 04:20:37

标签: ios swift uitableview autolayout uigesturerecognizer

我有一个带有子视图控制器(红色和蓝色)的视图控制器(绿色背景),如下所示:

enter image description here

我使用自动布局来确定子视图控制器的位置,如下所示:

color(0, 0, 0, 0); //transparent
color(0, 0, 0, 128); //half transparent
color(0, 0, 0, 255); //opaque (default)

此外,我在子视图控制器中设置自动布局约束以定位其子视图(tableHeaderView和tableView),如下所示:

func addConsttraints(){
    childController.view.translatesAutoresizingMaskIntoConstraints = false
    childController.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.6).isActive = true
    childController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    childController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    childController.view.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: 50).isActive = true
}

现在我已经在我的子视图控制器的func addConstraints(){ tableHeaderView.translatesAutoresizingMaskIntoConstraints = false tableHeaderView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true tableHeaderView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true tableHeaderView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true tableHeaderView.heightAnchor.constraint(equalToConstant: 44).isActive = true tableView.translatesAutoresizingMaskIntoConstraints = false tableView.topAnchor.constraint(equalTo: tableHeaderView.bottomAnchor).isActive = true tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true } 上设置了一个平移手势识别器,并且能够垂直移动它:

view

我的问题是,有没有办法在平移手势识别器上设置约束,以便在移动与之关联的视图时具有最大/最小值?例如,我希望我的最小值是 func didPan(gesture: UIPanGestureRecognizer){ let translation = gesture.translation(in: self.view) switch gesture.state{ case .began: print("Pan gesture recognizer began!") case .changed: let newPoint = CGPoint(x: view.center.x, y: view.center.y + translation.y) view.center = newPoint default: print("Default!") } gesture.setTranslation(CGPoint.zero, in: self.view) } 位于屏幕底部时的最小值:

enter image description here

此时我不希望用户能够进一步向下移动视图控制器。

最后,我希望最大值是:

enter image description here

非常感谢任何帮助。

0 个答案:

没有答案