0
我试图获得滚动视差效果。以下代码是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
谢谢您提前!