无法以编程方式更改约束

Can't get the constraints to change programmatically

我正在尝试获得滚动视差效果。以下是 CollectionViewCell 的代码。

#import "NearbyViewCell.h"

@interface NearbyViewCell ()

@property (nonatomic) CGFloat parallaxOffset;

@end
@implementation NearbyViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.clipsToBounds = YES;

        self.locationImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)];
        self.locationImage.frame = self.contentView.bounds;
        self.locationImage.contentMode = UIViewContentModeScaleAspectFill;

        self.locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height - 50.0)];
        self.locationLabel.numberOfLines = 0;
        self.locationLabel.textAlignment = NSTextAlignmentCenter;
        self.locationLabel.textColor = [UIColor whiteColor];
        self.locationLabel.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0 /255.0 alpha:0.5];


        self.otherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, (frame.size.height - 50.0), frame.size.width, 50.0)];
        self.otherLabel.textAlignment = NSTextAlignmentCenter;
        self.otherLabel.textColor = [UIColor whiteColor];
        self.otherLabel.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0 /255.0 alpha:0.5];

        [self.contentView addSubview:self.locationImage];
        [self.contentView addSubview:self.locationLabel];
        [self.contentView addSubview:self.otherLabel];
    }
    return self;
}

以下代码是从 collectionViewController 中的 scrollViewDidScroll 调用的。约束是一个 IBOutlet 并在头文件中声明。

- (void)updateParallaxOffset:(CGRect)bounds {
    CGPoint center = CGPointMake(CGRectGetMidX(bounds),   CGRectGetMidY(bounds));
    CGPoint offsetFromCenter = CGPointMake(center.x - self.center.x, center.y - self.center.y);
    CGFloat maxVerticalOffset = (CGRectGetHeight(bounds) / 2) + (CGRectGetHeight(self.bounds) / 2);
    CGFloat scaleFactor = 4000 / maxVerticalOffset;

    self.parallaxOffset = -offsetFromCenter.y * scaleFactor;

    self.locationLabelCenterYConstraint.constant = self.parallaxOffset;
    NSLog(@"%f", self.locationLabelCenterYConstraint.constant);
    NSLog(@"%f", self.parallaxOffset);
}

@end

问题是 'parallaxOffset' 的值在变化,但 'locationLabelCenterYConstraint' 的值没有变化。 NSLog 的结果如下。

2016-01-13 22:55:43.926 Parallax[1597:382314] 0.000000
2016-01-13 22:55:43.926 Parallax[1597:382314] -161.566707
2016-01-13 22:55:43.926 Parallax[1597:382314] 0.000000
2016-01-13 22:55:43.926 Parallax[1597:382314] 1307.221542
2016-01-13 22:55:43.926 Parallax[1597:382314] 0.000000
2016-01-13 22:55:43.927 Parallax[1597:382314] 2776.009792
2016-01-13 22:55:44.108 Parallax[1597:382314] 0.000000
2016-01-13 22:55:44.108 Parallax[1597:382314] -3094.247246
2016-01-13 22:55:44.109 Parallax[1597:382314] 0.000000
2016-01-13 22:55:44.109 Parallax[1597:382314] -1625.458996
2016-01-13 22:55:44.110 Parallax[1597:382314] 0.000000
2016-01-13 22:55:44.110 Parallax[1597:382314] -156.670747
2016-01-13 22:55:44.110 Parallax[1597:382314] 0.000000
2016-01-13 22:55:44.111 Parallax[1597:382314] 1312.117503
2016-01-13 22:55:44.118 Parallax[1597:382314] 0.000000
2016-01-13 22:55:44.118 Parallax[1597:382314] 2780.905753

提前致谢!

我通过以编程方式而不是通过界面生成器添加约束来实现这一点。这现在工作正常。