简体   繁体   中英

How to use AutoLayout constraints of a uiview on another uiview?

I am making a card game on iOS which has a lot of animations. Sometimes when two animations were happening at the same time, the animated card would snap back to its original position. I found out that this was happening because I animated the center of the UIView and not its constraints. Now, I'm trying to animate the card with its constraints. The card has to be animated to the top of another card. I was wondering if theres a way to copy all the constraints of one UIView to that of another?

I tried doing

newConstraints = object1.constraints
object2.addConstraints(newConstraints)

and then animate it, but it didn't work.

I used the following code to move the card:

private func replaceConstraints(_ superview: UIView, ofView view1: AnyObject, toView view2: AnyObject) -> [NSLayoutConstraint]{
    var constraintsToRemove: [NSLayoutConstraint]  = []
    var constraintsNew_item1: [NSLayoutConstraint]  = []

    for constraint in superview.constraints
    {
        if (constraint.firstItem === view1)
        {
            constraintsToRemove.append(constraint)
            constraintsNew_item1.append(NSLayoutConstraint(item: view2, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute, multiplier: constraint.multiplier, constant: constraint.constant))
        }
    }

    superview.removeConstraints(constraintsToRemove);
    superview.addConstraints(constraintsNew_item1);
    return constraintsToRemove
    
}

followed by: self.view.layoutIfNeeded()

The card did animate to the required position but the card on top of which it was supposed to be placed, moved a little from its position giving me the following warning:

[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:0x6000030aaa80 UIView:0x7f8f8870af50.centerY == 0.5*UIView:0x7f8f8870b0c0.centerY   (active)>",
    "<NSLayoutConstraint:0x6000030a7e80 UIView:0x7f8f8870af50.centerY == 1.5*UIView:0x7f8f8870b0c0.centerY   (active)>",
    "<NSLayoutConstraint:0x6000030baf80 'UIView-Encapsulated-Layout-Height' UIView:0x7f8f8870b0c0.height == 667   (active)>"
)

Also, I'm doing most of my animations in a CardView Class so I wanted to know if theres a dynamic way of doing it.

Thanks.

I did it by tweaking the code a little:

private func replaceConstraints(_ superview: UIView, view1: AnyObject, view2: AnyObject) -> [NSLayoutConstraint]{
    var constraintsToRemove: [NSLayoutConstraint]  = []
    var constraintsNew_item1: [NSLayoutConstraint]  = []

    for constraint in superview.constraints
    {
        if (constraint.firstItem === view1)
        {
            constraintsNew_item1.append(NSLayoutConstraint(item: view2, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute, multiplier: constraint.multiplier, constant: constraint.constant))
        }
        else if (constraint.firstItem === view2)
        {
            constraintsToRemove.append(constraint)
        }
    }

    superview.removeConstraints(constraintsToRemove);
    superview.addConstraints(constraintsNew_item1);
    return constraintsToRemove
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM