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
謝謝您提前!