move button towards target using constraints, no reaction?

无人久伴 提交于 2020-01-07 05:53:06

问题


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

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

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

- (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];
}

回答1:


This must 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];
    }];
}



回答2:


What I typically do in this situation is the following.

Add two constraints to your scene. One where it's aligned left between the button and the "one" label. The second, where it's aligned left between the button and the "second" label (i.e. both values will be 0). These constraints will initially conflict with one another, and that's fine.

Add IBOutlets to your view controller for the NSLayoutConstraints and assign the two constraints we've created to those IBOutlets.

Set the constraint priority on your initial condition to 999 (i.e. constraint on left align to "one" should be 999). Set the constraint priority on the destination constraint to 998 (i.e. constraint on left align between button to "second" is 998). You'll now see that these constraints will no longer conflict. This is because the priority on one constraint overrides the other.

You may see where this is headed now. So when you want to animate the button between the constraints, swap the 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's view
         [self.view layoutIfNeeded];
     }];
}


来源:https://stackoverflow.com/questions/27593430/move-button-towards-target-using-constraints-no-reaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!