Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: f23163f937
Fetching contributors…

Cannot retrieve contributors at this time

201 lines (164 sloc) 6.458 kB
//
// PSTCollectionViewCell.m
// PSPDFKit
//
// Copyright (c) 2012 Peter Steinberger. All rights reserved.
//
#import "PSTCollectionView.h"
#import "PSTCollectionViewCell.h"
#import "PSTCollectionViewLayout.h"
@interface PSTCollectionReusableView() {
struct {
unsigned int inUpdateAnimation:1;
} _reusableViewFlags;
}
@property (nonatomic, copy) NSString *reuseIdentifier;
@property (nonatomic, unsafe_unretained) PSTCollectionView *collectionView;
@property (nonatomic, strong) PSTCollectionViewLayoutAttributes *layoutAttributes;
@end
@implementation PSTCollectionReusableView
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if((self = [super initWithCoder:aDecoder])) {
}
return self;
}
- (void)awakeFromNib {
self.reuseIdentifier = [self valueForKeyPath:@"reuseIdentifier"];
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
- (void)prepareForReuse {
self.layoutAttributes = nil;
}
- (void)applyLayoutAttributes:(PSTCollectionViewLayoutAttributes *)layoutAttributes {
if (layoutAttributes != _layoutAttributes) {
_layoutAttributes = layoutAttributes;
self.frame = layoutAttributes.frame;
self.hidden = layoutAttributes.isHidden;
self.layer.transform = layoutAttributes.transform3D;
self.layer.zPosition = layoutAttributes.zIndex;
self.layer.opacity = layoutAttributes.alpha;
// TODO more attributes
}
}
- (void)willTransitionFromLayout:(PSTCollectionViewLayout *)oldLayout toLayout:(PSTCollectionViewLayout *)newLayout {
_reusableViewFlags.inUpdateAnimation = YES;
}
- (void)didTransitionFromLayout:(PSTCollectionViewLayout *)oldLayout toLayout:(PSTCollectionViewLayout *)newLayout {
_reusableViewFlags.inUpdateAnimation = NO;
}
- (BOOL)isInUpdateAnimation {
return _reusableViewFlags.inUpdateAnimation;
}
- (void)setInUpdateAnimation:(BOOL)inUpdateAnimation {
_reusableViewFlags.inUpdateAnimation = inUpdateAnimation;
}
@end
@implementation PSTCollectionViewCell
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_backgroundView = [[UIView alloc] initWithFrame:self.bounds];
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_backgroundView];
_contentView = [[UIView alloc] initWithFrame:self.bounds];
_contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_contentView];
_menuGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(menuGesture:)];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
- (void)prepareForReuse {
self.layoutAttributes = nil;
self.selected = NO;
self.highlighted = NO;
}
- (void)setSelected:(BOOL)selected {
if (_collectionCellFlags.selected != selected) {
_collectionCellFlags.selected = selected;
[self updateBackgroundView];
}
}
- (void)setHighlighted:(BOOL)highlighted {
if (_collectionCellFlags.highlighted != highlighted) {
_collectionCellFlags.highlighted = highlighted;
[self updateBackgroundView];
}
}
- (void)updateBackgroundView {
BOOL shouldHighlight = (self.highlighted || self.selected);
_selectedBackgroundView.alpha = shouldHighlight ? 1.0f : 0.0f;
[self setHighlighted:shouldHighlight forViews:self.contentView.subviews];
}
- (void)setHighlighted:(BOOL)highlighted forViews:(id)subviews {
for (id view in subviews) {
if ([view respondsToSelector:@selector(setHighlighted:)]) {
[view setHighlighted:highlighted];
}
[self setHighlighted:highlighted forViews:[view subviews]];
}
}
- (void)menuGesture:(UILongPressGestureRecognizer *)recognizer {
NSLog(@"Not yet implemented: %@", NSStringFromSelector(_cmd));
}
- (void)setBackgroundView:(UIView *)backgroundView {
if (_backgroundView != backgroundView) {
[_backgroundView removeFromSuperview];
_backgroundView = backgroundView;
[self addSubview:_backgroundView];
[self sendSubviewToBack:_backgroundView];
}
}
- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView {
if (_selectedBackgroundView != selectedBackgroundView) {
[_selectedBackgroundView removeFromSuperview];
_selectedBackgroundView = selectedBackgroundView;
_selectedBackgroundView.frame = self.bounds;
_selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_selectedBackgroundView.alpha = self.selected ? 1.0f : 0.0f;
[self insertSubview:_selectedBackgroundView aboveSubview:_backgroundView];
}
}
- (BOOL)isSelected {
return _collectionCellFlags.selected;
}
- (BOOL)isHighlighted {
return _collectionCellFlags.highlighted;
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - PSTCollection/UICollection interoperability
#import <objc/runtime.h>
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
NSMethodSignature *sig = [super methodSignatureForSelector:selector];
if(!sig) {
NSString *selString = NSStringFromSelector(selector);
if ([selString hasPrefix:@"_"]) {
SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
sig = [super methodSignatureForSelector:cleanedSelector];
}
}
return sig;
}
- (void)forwardInvocation:(NSInvocation *)inv {
NSString *selString = NSStringFromSelector([inv selector]);
if ([selString hasPrefix:@"_"]) {
SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
if ([self respondsToSelector:cleanedSelector]) {
inv.selector = cleanedSelector;
[inv invokeWithTarget:self];
}
}else {
[super forwardInvocation:inv];
}
}
@end
Jump to Line
Something went wrong with that request. Please try again.