Add UIView and UILabel to UICollectionViewCell. Then Segue based on clicked cell index
- by JetSet
I am new to collection views in Objective-C. Can anyone tell me why I can't see my UILabel embedded in the transparent UIView and the best way to resolve. I want to also segue from the cell to several various UIViewControllers based on the selected index cell.
I am using GitHub project https://github.com/mayuur/MJParallaxCollectionView
Overall, in MJRootViewController.m I wanted to add a UIView with a transparency and a UILabel with details of the cell from a array.
MJCollectionViewCell.h
// MJCollectionViewCell.h
// RCCPeakableImageSample
//
// Created by Mayur on 4/1/14.
// Copyright (c) 2014 RCCBox. All rights reserved.
//
#import <UIKit/UIKit.h>
#define IMAGE_HEIGHT 200
#define IMAGE_OFFSET_SPEED 25
@interface MJCollectionViewCell : UICollectionViewCell
/*
image used in the cell which will be having the parallax effect
*/
@property (nonatomic, strong, readwrite) UIImage *image;
/*
Image will always animate according to the imageOffset provided. Higher the value means higher offset for the image
*/
@property (nonatomic, assign, readwrite) CGPoint imageOffset;
//@property (nonatomic,readwrite) UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (nonatomic,readwrite) NSString *text;
@property(nonatomic,readwrite) CGFloat x,y,width,height;
@property (nonatomic,readwrite) NSInteger lineSpacing;
@property (nonatomic, strong) IBOutlet UIView* overlayView;
@end
MJCollectionViewCell.m
//
// MJCollectionViewCell.m
// RCCPeakableImageSample
//
// Created by Mayur on 4/1/14.
// Copyright (c) 2014 RCCBox. All rights reserved.
//
#import "MJCollectionViewCell.h"
@interface MJCollectionViewCell()
@property (nonatomic, strong, readwrite) UIImageView *MJImageView;
@end
@implementation MJCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) [self setupImageView];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) [self setupImageView];
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#pragma mark - Setup Method
- (void)setupImageView
{
// Clip subviews
self.clipsToBounds = YES;
// Add image subview
self.MJImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, IMAGE_HEIGHT)];
self.MJImageView.backgroundColor = [UIColor redColor];
self.MJImageView.contentMode = UIViewContentModeScaleAspectFill;
self.MJImageView.clipsToBounds = NO;
[self addSubview:self.MJImageView];
}
# pragma mark - Setters
- (void)setImage:(UIImage *)image
{
// Store image
self.MJImageView.image = image;
// Update padding
[self setImageOffset:self.imageOffset];
}
- (void)setImageOffset:(CGPoint)imageOffset
{
// Store padding value
_imageOffset = imageOffset;
// Grow image view
CGRect frame = self.MJImageView.bounds;
CGRect offsetFrame = CGRectOffset(frame, _imageOffset.x, _imageOffset.y);
self.MJImageView.frame = offsetFrame;
}
- (void)setText:(NSString *)text{
_text=text;
if (!self.textLabel) {
CGFloat realH=self.height*2/3-self.lineSpacing;
CGFloat latoA=realH/3;
// self.textLabel=[[UILabel alloc] initWithFrame:CGRectMake(10,latoA/2, self.width-20, realH)];
self.textLabel.layer.anchorPoint=CGPointMake(.5, .5);
self.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-ultralight" size:38];
self.textLabel.numberOfLines=3;
self.textLabel.textColor=[UIColor whiteColor];
self.textLabel.shadowColor=[UIColor blackColor];
self.textLabel.shadowOffset=CGSizeMake(1, 1);
self.textLabel.transform=CGAffineTransformMakeRotation(-(asin(latoA/(sqrt(self.width*self.width+latoA*latoA)))));
[self addSubview:self.textLabel];
}
self.textLabel.text=text;
}
@end
MJViewController.h
//
// MJViewController.h
// ParallaxImages
//
// Created by Mayur on 4/1/14.
// Copyright (c) 2014 sky. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MJRootViewController : UIViewController{
NSInteger choosed;
}
@end
MJViewController.m
//
// MJViewController.m
// ParallaxImages
//
// Created by Mayur on 4/1/14.
// Copyright (c) 2014 sky. All rights reserved.
//
#import "MJRootViewController.h"
#import "MJCollectionViewCell.h"
@interface MJRootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *parallaxCollectionView;
@property (nonatomic, strong) NSMutableArray* images;
@end
@implementation MJRootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.navigationController.navigationBarHidden=YES;
// Fill image array with images
NSUInteger index;
for (index = 0; index < 14; ++index) {
// Setup image name
NSString *name = [NSString stringWithFormat:@"image%03ld.jpg", (unsigned long)index];
if(!self.images)
self.images = [NSMutableArray arrayWithCapacity:0];
[self.images addObject:name];
}
[self.parallaxCollectionView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionViewDatasource Methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MJCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MJCell" forIndexPath:indexPath];
//get image name and assign
NSString* imageName = [self.images objectAtIndex:indexPath.item];
cell.image = [UIImage imageNamed:imageName];
//set offset accordingly
CGFloat yOffset = ((self.parallaxCollectionView.contentOffset.y - cell.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
cell.imageOffset = CGPointMake(0.0f, yOffset);
NSString *text;
NSInteger index=choosed>=0 ? choosed : indexPath.row%5;
switch (index) {
case 0:
text=@"I am the home cell...";
break;
case 1:
text=@"I am next...";
break;
case 2:
text=@"Cell 3...";
break;
case 3:
text=@"Cell 4...";
break;
case 4:
text=@"The last cell";
break;
default:
break;
}
cell.text=text;
cell.overlayView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
//cell.textLabel.text = @"Label showing";
cell.textLabel.font = [UIFont boldSystemFontOfSize:22.0f];
cell.textLabel.textColor = [UIColor whiteColor];
//This is another attempt to display the label by using tags.
//UILabel* label = (UILabel*)[cell viewWithTag:1];
//label.text = @"Label works";
return cell;
}
#pragma mark - UIScrollViewdelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for(MJCollectionViewCell *view in self.parallaxCollectionView.visibleCells) {
CGFloat yOffset = ((self.parallaxCollectionView.contentOffset.y - view.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
view.imageOffset = CGPointMake(0.0f, yOffset);
}
}
@end