Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d7beac5e39
Fetching contributors…

Cannot retrieve contributors at this time

file 192 lines (140 sloc) 4.85 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//
// SSCollectionViewItem.m
// SSToolkit
//
// Created by Sam Soffes on 6/11/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//

#import "SSCollectionViewItem.h"
#import "SSCollectionViewItemInternal.h"
#import "SSCollectionView.h"
#import "SSCollectionViewInternal.h"
#import "SSLabel.h"
#import "SSDrawingUtilities.h"

@implementation SSCollectionViewItem {
SSCollectionViewItemStyle _style;
}


#pragma mark - Accessors

@synthesize imageView = _imageView;
@synthesize textLabel = _textLabel;
@synthesize detailTextLabel = _detailTextLabel;
@synthesize backgroundView = _backgroundView;
@synthesize selectedBackgroundView = _selectedBackgroundView;
@synthesize reuseIdentifier = _reuseIdentifier;
@synthesize selected = _selected;
@synthesize highlighted = _highlighted;
@synthesize indexPath = _indexPath;
@synthesize collectionView = _collectionView;

- (void)setBackgroundView:(UIView *)backgroundView {
_backgroundView = backgroundView;
_backgroundView.hidden = _selected && _selectedBackgroundView;

[self insertSubview:backgroundView atIndex:0];
[self setNeedsLayout];
}


- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView {
_selectedBackgroundView = selectedBackgroundView;
_selectedBackgroundView.hidden = !_selected;

if (_backgroundView) {
[self insertSubview:_selectedBackgroundView aboveSubview:_backgroundView];
} else {
[self insertSubview:_selectedBackgroundView atIndex:0];
}

[self setNeedsLayout];
}


#pragma mark - NSObject

- (void)dealloc {
self.collectionView = nil;
}


#pragma mark - UIResponder

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:YES animated:NO];
}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:NO animated:NO];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setHighlighted:YES animated:NO];

if (CGRectContainsPoint(self.bounds, [[touches anyObject] locationInView:self])) {
        if (self.isSelected) {
            [self.collectionView deselectItemAtIndexPath:self.indexPath animated:YES];
        } else {
            [self.collectionView selectItemAtIndexPath:self.indexPath animated:YES scrollPosition:SSCollectionViewScrollPositionNone];
        }
}
}


#pragma mark - UIView

- (id)initWithFrame:(CGRect)frame {
    return [self initWithStyle:SSCollectionViewItemStyleDefault reuseIdentifier:nil];
}


- (void)layoutSubviews {
_backgroundView.frame = self.bounds;
_selectedBackgroundView.frame = self.bounds;

if (_style == SSCollectionViewItemStyleImage) {
_imageView.frame = self.bounds;
} else if (_style == SSCollectionViewItemStyleDefault) {
_textLabel.frame = self.bounds;
}
}


#pragma mark - SSCollectionViewItem

- (id)initWithStyle:(SSCollectionViewItemStyle)style reuseIdentifier:(NSString *)aReuseIdentifier {
if ((self = [super initWithFrame:CGRectZero])) {
_style = style;
_reuseIdentifier = [aReuseIdentifier copy];

if (_style != SSCollectionViewItemStyleBlank) {
if (_style == SSCollectionViewItemStyleSubtitle) {
_detailTextLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
_detailTextLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_detailTextLabel];
}

_textLabel = [[SSLabel alloc] initWithFrame:CGRectZero];
_textLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_textLabel];

_imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self addSubview:_imageView];
}

    }
    return self;
}


- (void)prepareForReuse {
// Do nothing. Subclasses can override this
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
_selected = selected;

void (^changes)(void) = ^{
for (UIView *view in [self subviews]) {
if ([view respondsToSelector:@selector(setSelected:)]) {
[(UIControl *)view setSelected:_selected];
}
}

_backgroundView.hidden = _selected && _selectedBackgroundView;
_selectedBackgroundView.hidden = !_selected;
};

if (animated) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:nil];
} else {
changes();
}
}


- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
_highlighted = highlighted;

void (^changes)(void) = ^{
for (UIView *view in [self subviews]) {
if ([view respondsToSelector:@selector(setHighlighted:)]) {
[(UIControl *)view setHighlighted:_highlighted];
}
}
};

if (animated) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:nil];
} else {
changes();
}
}


#pragma mark Setters

- (void)setSelected:(BOOL)selected {
[self setSelected:selected animated:YES];
}


- (void)setHighlighted:(BOOL)selected {
[self setHighlighted:selected animated:YES];
}

@end
Something went wrong with that request. Please try again.