我有一个UIView
具有相应的子类xib
文件。在我的 xib 中我有一个NSLayoutConstraint
我正在尝试为其设置动画的属性。我有一个animateIn
方法。问题是只有 animateIn 方法有效。当我尝试再次更新常量时,它只是保持以前的值。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalConstraint;
我试图在按下按钮后更新常量。但常数设置后似乎没有更新。即使将其设置为 -500 后,它仍然记录 0。我正在打电话layoutIfNeeded
但什么也没发生。
// this works
- (void) animateIn {
[UIView animateWithDuration:1.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.horizontalConstraint.constant = 0;
[self layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}];
}
// this does not work
- (IBAction)resume:(id)sender {
self.horizontalConstraint.constant = -500;
[self layoutIfNeeded];
NSLog(@"%f",self.horizontalConstraint.constant); // this always stays 0
}
UPDATE
当我想第二次使用它时,我的 NSLayoutConstraint 似乎是 (null) 。这可以解释为什么它不更新。我应该如何保留对它的引用?
您需要致电setNeedsUpdateConstraints
对应的方法UIView(control)
其中您的 NSLayoutConstraint 用于更新约束。
以 UIButton 为例
self.buttonConstraint.constant = 55;
[self.btnTest setNeedsUpdateConstraints];
在你的情况下
[self setNeedsUpdateConstraints];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)