无法同时满足动画约束

时间:2019-07-03 11:54:07

标签: ios swift autolayout

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

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.

1 个答案:

答案 0 :(得分:2)

之所以会这样,是因为您要先激活新的(冲突的)约束,然后再禁用已经生效的旧约束。

问题else的开始,您需要先 禁用其他用户,然后再设置tennisButtonHiddenConstraint1和{{1} }激活:

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 为第一准则。