Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改NSLayoutConstraint的常量 背景_Ios_Swift_Uiview_Nslayoutconstraint - Fatal编程技术网

Ios 更改NSLayoutConstraint的常量 背景

Ios 更改NSLayoutConstraint的常量 背景,ios,swift,uiview,nslayoutconstraint,Ios,Swift,Uiview,Nslayoutconstraint,我正在尝试使用动画和非动画选项进行相当标准的NSLayoutConstraint常量更新。为了让您了解一些背景知识,我有一个名为ProgressView的UIView子类。在ProgressView中,有进度条视图。进度条UIView作为引用插座progressBar连接到ProgressView类。下面是故事板中相当简单的层次结构的外观: 正如您可能已经猜到的,我正在构建一个自定义进度条。ProgressView UIView是“轨迹”,而进度条UIView是“填充”。计划是使用进度条UIV

我正在尝试使用动画和非动画选项进行相当标准的NSLayoutConstraint常量更新。为了让您了解一些背景知识,我有一个名为ProgressView的UIView子类。在ProgressView中,有进度条视图。进度条UIView作为引用插座
progressBar
连接到ProgressView类。下面是故事板中相当简单的层次结构的外观:

正如您可能已经猜到的,我正在构建一个自定义进度条。ProgressView UIView是“轨迹”,而进度条UIView是“填充”。计划是使用进度条UIView的尾部约束对进度进行更改


现有配置 进度条UIView的约束设置为与superview(进度视图)齐平:

进度条UIView的尾部约束作为强出口连接到ProgressView类:
@IBOutlet var trailingConstraint:NSLayoutConstraint

在ProgressView类中,有一个
setProgress
函数。此函数从包含ProgressView的ViewController调用。下面是带有注释的函数,解释其工作原理:

 public func setProgress(_ progress: Double, animationDuration: Double) {

    // Pre-conditions
    guard progress >= 0.0 && progress <= 1.0 && animationDuration >= 0.0 else {
        return
    }

    // Calculate the new constraint constant based on the progress
    let newConstant = CGFloat(1 - progress) * self.bounds.size.width

    // If the update should be made without animation, just make the change and return
    guard animationDuration > 0.0 else {
        trailingConstraint.constant = newConstant            
        return
    }

    // Set the constraint first
    self.trailingConstraint.constant = newConstant

    // Animate!
    UIView.animate(withDuration: animationDuration) {
        self.layoutIfNeeded()
    }

}
public func setProgress(uprogress:Double,animationDuration:Double){
//先决条件
防护进度>=0.0&&progress=0.0其他{
返回
}
//根据进度计算新的约束常量
设newConstant=CGFloat(1-进度)*self.bounds.size.width
//如果应该在没有动画的情况下进行更新,只需进行更改并返回
保护动画持续时间>0.0其他{
trailingConstraint.constant=新常量
返回
}
//首先设置约束
self.trailingConstraint.constant=newConstant
//动画!
UIView.animate(withDuration:animationDuration){
self.layoutifneed()
}
}

问题 如您所见,此函数可以在有动画和无动画的情况下更新进度。但是,当从ViewController调用时,两者都不起作用。尾部约束常量似乎保持为0,进度条将填充整个轨迹


尝试的解决方案 我尝试在每个位置和配置中调用
layoutifneed()
setNeedsLayout()
(在
self
[其中
self
是ProgressView]和
progressBar
上)。我已经尝试使用
DispatchQueue.main.async
将代码显式地放在主线程上。作为地面真实性测试,我尝试将trailingConstraint出口连接到ViewController本身,并从
ViewDidAspect()
viewDidLayoutSubviews()
方法更新常量。什么都不管用


正确方向的可能暗示 这是奇怪的部分。进度条在屏幕上显示为已填充,但在调试视图层次结构时,进度条看起来是正确的。trailingConstraint常量似乎已正确设置


我在iOS10设备上测试,运行的是Xcode 8.3.3。这个问题对我来说真的很奇怪,我不太确定接下来该怎么办。谢谢您的帮助。

我也遇到了同样的问题。通过在动画中更改常量,我的问题得到了解决。请尝试以下操作:

 UIView.animate(withDuration: 0.2) { 
    self.trailingConstraint.constant = newConstant
    self.layoutIfNeeded()
    }

我也遇到了同样的问题。通过更改动画中的常数,我的问题得到了解决。请尝试以下操作:

 UIView.animate(withDuration: 0.2) { 
    self.trailingConstraint.constant = newConstant
    self.layoutIfNeeded()
    }

我尝试过在动画外部使用layoutifneed和constant,在内部使用layoutifneed。我已经试过了,需要在里面放一放。不幸的是,没有一个成功。将这一行self.trailingConstraint.constant=newConstant放在动画块之外。您是否将动画持续时间设置为0.2秒。试试看。这就像一种hack@Dev_Tandel我试过了。不幸的是,它不起作用。在我的setProgress方法中,我在保护中还有一个非动画约束更改。这也不行。@Arnav我尝试了一个硬编码的持续时间值(2秒)。没用。0.2秒会有所不同吗?我已经尝试了LayoutFneed和constant在动画外部,LayoutFneed在内部。我已经试过了,需要在里面放一放。不幸的是,没有一个成功。将这一行self.trailingConstraint.constant=newConstant放在动画块之外。您是否将动画持续时间设置为0.2秒。试试看。这就像一种hack@Dev_Tandel我试过了。不幸的是,它不起作用。在我的setProgress方法中,我在保护中还有一个非动画约束更改。这也不行。@Arnav我尝试了一个硬编码的持续时间值(2秒)。没用。0.2秒会有所不同吗?