gpt4 book ai didi

ios - 使用约束将按钮移向目标,没有反应?

转载 作者:行者123 更新时间:2023-11-28 21:53:03 25 4
gpt4 key购买 nike

我希望红色按钮动画朝向第二个按钮的领先位置:

enter image description here

一些例子展示了如何用数字改变“常量”,但我想自动放在第二个按钮的前导位置。

我试过了,但是红色按钮没有移动,不过动画日志被正确调用了:

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

最佳答案

必须这样做:

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

关于ios - 使用约束将按钮移向目标,没有反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593430/

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