我正在尝试获得滚动视差效果。以下代码属于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
提前谢谢你!
答案 0 :(得分:0)
我通过以编程方式而不是通过界面构建器添加约束来实现此目的。现在效果很好。