gpt4 book ai didi

ios - 无法同时满足动画约束

转载 作者:搜寻专家 更新时间:2023-11-01 06:51:34 25 4
gpt4 key购买 nike

我有一个按钮,当按下它时,它会旋转显示最初使用自动布局隐藏在它后面的其他按钮。

enter image description here enter image description here

添加的按钮具有以下约束:

var tennisButtonHiddenConstraint1 : NSLayoutConstraint?
var tennisButtonHiddenConstraint2 : NSLayoutConstraint?
var tennisButtonBottomAnchor : NSLayoutConstraint?
var tennisButtonRightAnchor : NSLayoutConstraint?
var tennisButtonWidthAnchor : NSLayoutConstraint?
var tennisButtonHeightAnchor : NSLayoutConstraint?


func setupViews(){
self.mapView.addSubview(tennisButton)
tennisButtonHiddenConstraint1 = tennisButton.bottomAnchor.constraint(equalTo: self.mapView.bottomAnchor, constant: -50)
tennisButtonHiddenConstraint1?.isActive = true

tennisButtonHiddenConstraint2 = tennisButton.centerXAnchor.constraint(equalTo: self.mapView.centerXAnchor)
tennisButtonHiddenConstraint2?.isActive = true

tennisButtonRightAnchor = tennisButton.rightAnchor.constraint(equalTo: self.addEventButton.leftAnchor, constant: -20)
tennisButtonRightAnchor?.isActive = false

tennisButtonBottomAnchor = tennisButton.bottomAnchor.constraint(equalTo: self.addEventButton.topAnchor, constant: 20)
tennisButtonBottomAnchor?.isActive = false

tennisButtonWidthAnchor = tennisButton.widthAnchor.constraint(equalToConstant: 66)
tennisButtonWidthAnchor?.isActive = false
tennisButtonHeightAnchor = tennisButton.heightAnchor.constraint(equalToConstant: 66)
tennisButtonHeightAnchor?.isActive = false

self.mapView.addSubview(addEventButton)
addEventButton.bottomAnchor.constraint(equalTo: self.mapView.bottomAnchor, constant: -50).isActive = true
addEventButton.centerXAnchor.constraint(equalTo: self.mapView.centerXAnchor).isActive = true
addEventButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
addEventButton.heightAnchor.constraint(equalToConstant: 80).isActive = true
}

然后动画代码如下:

@objc func showAvailableActions(){
if menuButtonShown == false {
self.tennisButtonHiddenConstraint1?.isActive = false
self.tennisButtonHiddenConstraint2?.isActive = false
self.tennisButtonBottomAnchor?.isActive = true
self.tennisButtonRightAnchor?.isActive = true
self.tennisButtonHeightAnchor?.isActive = true
self.tennisButtonWidthAnchor?.isActive = true
menuButtonShown = true

UIView.animate(withDuration: 0.5, animations: {
self.addEventButton.transform = CGAffineTransform(rotationAngle: .pi/4)
})

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)

}else{

self.tennisButtonHiddenConstraint1?.isActive = true
self.tennisButtonHiddenConstraint2?.isActive = true
self.tennisButtonBottomAnchor?.isActive = false
self.tennisButtonRightAnchor?.isActive = false
self.tennisButtonHeightAnchor?.isActive = false
self.tennisButtonWidthAnchor?.isActive = false
menuButtonShown = false


UIView.animate(withDuration: 0.5, animations: {
self.addEventButton.transform = CGAffineTransform.identity
})
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)

}
}

当显示按钮时,一切正常,但是当它隐藏时,我得到以下信息:

Yoofit[7442:9349648] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000a10f50 Yoofit.AddEventButton:0x7f8584c3beb0.height == 80 (active)>",
"<NSLayoutConstraint:0x600000a17520 UIButton:0x7f8584c3c3e0.bottom == Yoofit.AddEventButton:0x7f8584c3beb0.top + 20 (active)>",
"<NSLayoutConstraint:0x600000a17750 Yoofit.AddEventButton:0x7f8584c3beb0.bottom == MKMapView:0x7f858505f200.bottom - 50 (active)>",
"<NSLayoutConstraint:0x600000a6b980 UIButton:0x7f8584c3c3e0.bottom == MKMapView:0x7f858505f200.bottom - 50 (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000a10f50 Yoofit.AddEventButton:0x7f8584c3beb0.height == 80 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-07-03 19:51:02.337695+0800 Yoofit[7442:9349648] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000a17930 Yoofit.AddEventButton:0x7f8584c3beb0.width == 80 (active)>",
"<NSLayoutConstraint:0x600000a17430 UIButton:0x7f8584c3c3e0.right == Yoofit.AddEventButton:0x7f8584c3beb0.left - 20 (active)>",
"<NSLayoutConstraint:0x600000a177f0 Yoofit.AddEventButton:0x7f8584c3beb0.centerX == MKMapView:0x7f858505f200.centerX (active)>",
"<NSLayoutConstraint:0x600000a174d0 UIButton:0x7f8584c3c3e0.width == 66 (active)>",
"<NSLayoutConstraint:0x600000a6bf20 UIButton:0x7f8584c3c3e0.centerX == MKMapView:0x7f858505f200.centerX (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000a17930 Yoofit.AddEventButton:0x7f8584c3beb0.width == 80 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-07-03 19:51:02.341441+0800 Yoofit[7442:9349648] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000a17430 UIButton:0x7f8584c3c3e0.right == Yoofit.AddEventButton:0x7f8584c3beb0.left - 20 (active)>",
"<NSLayoutConstraint:0x600000a177f0 Yoofit.AddEventButton:0x7f8584c3beb0.centerX == MKMapView:0x7f858505f200.centerX (active)>",
"<NSLayoutConstraint:0x600000a174d0 UIButton:0x7f8584c3c3e0.width == 66 (active)>",
"<NSLayoutConstraint:0x600000a6bf20 UIButton:0x7f8584c3c3e0.centerX == MKMapView:0x7f858505f200.centerX (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000a174d0 UIButton:0x7f8584c3c3e0.width == 66 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-07-03 19:51:02.360114+0800 Yoofit[7442:9349648] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000a17430 UIButton:0x7f8584c3c3e0.right == Yoofit.AddEventButton:0x7f8584c3beb0.left - 20 (active)>",
"<NSLayoutConstraint:0x600000a177f0 Yoofit.AddEventButton:0x7f8584c3beb0.centerX == MKMapView:0x7f858505f200.centerX (active)>",
"<NSLayoutConstraint:0x600000a6bf20 UIButton:0x7f8584c3c3e0.centerX == MKMapView:0x7f858505f200.centerX (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000a177f0 Yoofit.AddEventButton:0x7f8584c3beb0.centerX == MKMapView:0x7f858505f200.centerX (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

最佳答案

您看到这种情况的原因是您在禁用已经生效的旧约束之前激活了新的(冲突的)约束。

else block 的开头是问题所在,您需要在设置 tennisButtonHiddenConstraint1 之前首先 禁用其他 block > 和 tennisButtonHiddenConstraint2 激活:

    self.tennisButtonBottomAnchor?.isActive = false
self.tennisButtonRightAnchor?.isActive = false
self.tennisButtonHeightAnchor?.isActive = false
self.tennisButtonWidthAnchor?.isActive = false
self.tennisButtonHiddenConstraint1?.isActive = true
self.tennisButtonHiddenConstraint2?.isActive = true

它在首次加载时起作用,因为尚未添加其他约束(因此顺序无关紧要)。

如果有疑问,在约束集之间切换时,始终首先设置 .isActive = false 是我的经验法则。

关于ios - 无法同时满足动画约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869564/

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