Move the button to the target using restrictions, no reaction?

I want the red button to be animated towards the leading position of the second button:

enter image description here

Some examples show how to change the "constant" with numbers, but I would like to automatically put it in the leading position of the second button.

I tried this, but the red button does not move, the animation log is correctly called:

- (void)updateConstr{ NSLayoutConstraint *newLeading = [NSLayoutConstraint constraintWithItem:self.redB attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.secondButton attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f]; self.leadingConstraint = newLeading;//is an IBOutlet pointing on the constraint (see the image) [self.redB setNeedsUpdateConstraints]; [UIView animateWithDuration:0.5 animations:^{ [self.redB layoutIfNeeded]; NSLog(@"animations");//is called, but the red button does not move }]; } - (IBAction)firstAction:(id)sender { //after a click on button "one" NSLog(@"firstAction"); [self updateConstr]; } 
0
source share
2 answers

This should do it:

 - (void)updateConstr{ NSLayoutConstraint *newLeading = [NSLayoutConstraint constraintWithItem:self.redB attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.secondButton attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f]; [self.redB.superview removeConstraint: self.leadingConstraint]; [self.redB.superview addConstraint: newLeading]; self.leadingConstraint = newLeading; [UIView animateWithDuration:0.5 animations:^{ [self.redB layoutIfNeeded]; }]; } 
+1
source

What I usually do in this situation is the following.

Add two limitations to your scene. One where it is aligned to the left of the button and the "single" label. The second one, where it is aligned to the left of the button and the “second” label (ie Both values ​​will be 0). These restrictions first conflict with each other, and that’s fine.

Add IBOutlets to the view controller for NSLayoutConstraints and assign the two constraints that we created to those << 20>.

Set the restriction priority for your initial condition to 999 (that is, the restriction in the left match with "one" should be 999). Set the priority of the restriction to limit the assignment to 998 (i.e., the restriction on the left alignment between the buttons on the "second" is 998). You will now see that these restrictions will no longer conflict. This is due to the fact that the priority of one restriction overrides another.

You can see where it is heading. Therefore, when you want to animate a button between restrictions, change priorities and animate!

code:

 @interface MyViewController () @property (nonatomic, weak) NSLayoutConstraint* constraint0; @property (nonatomic, weak) NSLayoutConstraint* constraint1; @end - (void)someMethodWhereIWantToAnimate { NSInteger temp = constraint0.priority; constraint0.priority = constraint1.priority; constraint1.priority = temp; [UIView animateWithDuration:1.0 animations:^{ // Simplest is to use the main ViewController view [self.view layoutIfNeeded]; }]; } 
+1
source

Source: https://habr.com/ru/post/983370/


All Articles