Ios 拖动时调整UIView框架

Ios 拖动时调整UIView框架,ios,swift,uiview,constraints,touch-event,Ios,Swift,Uiview,Constraints,Touch Event,我正在尝试在拖动时更改UIView帧。我将UIView子类化,并编写了TouchesBegind和touchesMoved方法。这是: override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch = touches.first startPosition = touch?.locationInView(self) } override func to

我正在尝试在拖动时更改UIView帧。我将UIView子类化,并编写了TouchesBegind和touchesMoved方法。这是:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first
    startPosition = touch?.locationInView(self)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first
    let endPosition = touch?.locationInView(self)
    let difference = endPosition!.y - startPosition.y
    let newFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.width, self.frame.height + difference)
    self.frame = newFrame
}
但结果我有了意想不到的行为。这有点难以形容。小dragabout 20px使帧在大约100px上变大

我的自定义视图也有一些限制。自定义视图底部的图像应始终保持不变:

因此,问题是:

如何在用户拖动的距离上使视图变大。 为什么在更改框架后不应用约束以及如何再次应用约束?
视图快速增长的原因是,您总是根据第一次触地后移动的距离更新当前视图高度。应始终添加移动到视图原始高度的距离,而不是修改当前高度

class ViewController: UIViewController {

    @IBOutlet weak var customViewHeight: NSLayoutConstraint!

    @IBOutlet weak var customView: CustomView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Assign the layout constraint to the custom view so that it can update it itself
        customView.customViewHeight = customViewHeight
    }
}

class CustomView: UIView {

    var startPosition: CGPoint?
    var originalHeight: CGFloat = 0
    var customViewHeight: NSLayoutConstraint!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        startPosition = touch?.locationInView(self)
        originalHeight = customViewHeight.constant
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let endPosition = touch?.locationInView(self)
        let difference = endPosition!.y - startPosition!.y
        customViewHeight.constant = originalHeight + difference
    }

}
向自定义视图添加另一个特性,以跟踪视图的原始高度。在touchsbegined中设置,然后在touchsmoved中计算帧时使用该原始高度:


在您的例子中,我认为您应该更改底部约束的常量,而不是更改视图的框架。另外,您最好使用手势,而不是覆盖触摸方法,因为手势是更高级的API,更易于维护

基本上,使用自动布局的项目应该避免使用框架更改视图

class ViewController: UIViewController {

    @IBOutlet weak var customViewHeight: NSLayoutConstraint!

    @IBOutlet weak var customView: CustomView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Assign the layout constraint to the custom view so that it can update it itself
        customView.customViewHeight = customViewHeight
    }
}

class CustomView: UIView {

    var startPosition: CGPoint?
    var originalHeight: CGFloat = 0
    var customViewHeight: NSLayoutConstraint!

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        startPosition = touch?.locationInView(self)
        originalHeight = customViewHeight.constant
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let endPosition = touch?.locationInView(self)
        let difference = endPosition!.y - startPosition!.y
        customViewHeight.constant = originalHeight + difference
    }

}