gpt4 book ai didi

ios - 拖动时调整 UIView 框架

转载 作者:可可西里 更新时间:2023-11-01 00:52:16 26 4
gpt4 key购买 nike

我试图在拖动时更改 UIView 框架。我继承了 UIView 并编写了 touchesBegantouchesMoved 方法。他们在这里:

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
}

但结果我得到了意想不到的行为。描述起来有点困难。 Here's video how it works. .小阻力(约 20 像素)使框架变大约 100 像素。

我的自定义 View 也有一些限制。自定义 View 底部的图像应始终保留在那里:

enter image description here

所以问题是:

  1. 如何根据用户拖动的距离使 View 变大。
  2. 为什么更改框架后约束不适用以及如何再次应用它们?

最佳答案

您的 View 快速增长的原因是您始终使用自第一次触地后移动的距离更新当前 View 高度。您不应修改当前高度,而应始终将移动的距离添加到 View 的原始高度。

向您的自定义 View 添加另一个属性以跟踪 View 的原始高度。在 touchesBegan 中设置它,然后在 touchesMoved 中计算框架时使用 originalHeight:

class CustomView: UIView {

var startPosition: CGPoint?
var originalHeight: CGFloat = 0

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

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, originalHeight + difference)
self.frame = newFrame
}

}

And why constraints do not apply after changing frame and how to apply them again?

您真的不应该在使用自动布局时更新框架。执行此操作的正确方法是将 @IBOutlet 添加到 View 的高度约束,然后在您希望更改 View 的高度时更新约束的 constant 属性你的看法。

当您这样做时,自动布局将使用更新后的高度正确定位自定义 View 的 subview 。

要将 @IBOutlet 添加到代码的高度限制,Control - 从 Document Outline 中的高度限制拖动到您的 View Controller 。为其命名,例如 customViewHeight

Control drag to create @IBOutlet for height constraint

由于更新 View 高度的代码在 View 代码本身中,因此让 viewController 将高度约束分配给 viewDidLoad 中的自定义 View 。然后自定义 View 可以更新高度约束的 constant 属性来调整 View 的大小。

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
}

}

关于ios - 拖动时调整 UIView 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33581638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com