I am trying to get the scrolling parallax effect. The following code is of the 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;
}
The following code is called from the scrollViewDidScroll in the collectionViewController. The constraint is an IBOutlet and is declared in the header file.
- (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
The problem is that the value of 'parallaxOffset' is changing but the value of 'locationLabelCenterYConstraint' is not changing. The result of the NSLog is given below.
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
Thank You in advance!
I have got this to work by adding the constraint programmatically rather than through interface builder. This works fine now.