PSTCollectionViewCell.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // PSTCollectionViewCell.m
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTCollectionView.h"
  8. @interface PSTCollectionReusableView () {
  9. PSTCollectionViewLayoutAttributes *_layoutAttributes;
  10. NSString *_reuseIdentifier;
  11. __unsafe_unretained PSTCollectionView *_collectionView;
  12. struct {
  13. unsigned int inUpdateAnimation : 1;
  14. }_reusableViewFlags;
  15. char filler[50]; // [HACK] Our class needs to be larger than Apple's class for the superclass change to work.
  16. }
  17. @property (nonatomic, copy) NSString *reuseIdentifier;
  18. @property (nonatomic, unsafe_unretained) PSTCollectionView *collectionView;
  19. @property (nonatomic, strong) PSTCollectionViewLayoutAttributes *layoutAttributes;
  20. @end
  21. @implementation PSTCollectionReusableView
  22. ///////////////////////////////////////////////////////////////////////////////////////////
  23. #pragma mark - NSObject
  24. - (id)initWithFrame:(CGRect)frame {
  25. if ((self = [super initWithFrame:frame])) {
  26. }
  27. return self;
  28. }
  29. - (id)initWithCoder:(NSCoder *)aDecoder {
  30. if ((self = [super initWithCoder:aDecoder])) {
  31. self.reuseIdentifier = [aDecoder decodeObjectForKey:@"UIReuseIdentifier"];
  32. }
  33. return self;
  34. }
  35. - (void)awakeFromNib {
  36. self.reuseIdentifier = [self valueForKeyPath:@"reuseIdentifier"];
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////////////////
  39. #pragma mark - Public
  40. - (void)prepareForReuse {
  41. self.layoutAttributes = nil;
  42. }
  43. - (void)applyLayoutAttributes:(PSTCollectionViewLayoutAttributes *)layoutAttributes {
  44. if (layoutAttributes != _layoutAttributes) {
  45. _layoutAttributes = layoutAttributes;
  46. self.bounds = (CGRect){.origin = self.bounds.origin, .size = layoutAttributes.size};
  47. self.center = layoutAttributes.center;
  48. self.hidden = layoutAttributes.hidden;
  49. self.layer.transform = layoutAttributes.transform3D;
  50. self.layer.zPosition = layoutAttributes.zIndex;
  51. self.layer.opacity = layoutAttributes.alpha;
  52. }
  53. }
  54. - (void)willTransitionFromLayout:(PSTCollectionViewLayout *)oldLayout toLayout:(PSTCollectionViewLayout *)newLayout {
  55. _reusableViewFlags.inUpdateAnimation = YES;
  56. }
  57. - (void)didTransitionFromLayout:(PSTCollectionViewLayout *)oldLayout toLayout:(PSTCollectionViewLayout *)newLayout {
  58. _reusableViewFlags.inUpdateAnimation = NO;
  59. }
  60. - (BOOL)isInUpdateAnimation {
  61. return _reusableViewFlags.inUpdateAnimation;
  62. }
  63. - (void)setInUpdateAnimation:(BOOL)inUpdateAnimation {
  64. _reusableViewFlags.inUpdateAnimation = (unsigned int)inUpdateAnimation;
  65. }
  66. @end
  67. @implementation PSTCollectionViewCell {
  68. UIView *_contentView;
  69. UIView *_backgroundView;
  70. UIView *_selectedBackgroundView;
  71. UILongPressGestureRecognizer *_menuGesture;
  72. id _selectionSegueTemplate;
  73. id _highlightingSupport;
  74. struct {
  75. unsigned int selected : 1;
  76. unsigned int highlighted : 1;
  77. unsigned int showingMenu : 1;
  78. unsigned int clearSelectionWhenMenuDisappears : 1;
  79. unsigned int waitingForSelectionAnimationHalfwayPoint : 1;
  80. }_collectionCellFlags;
  81. BOOL _selected;
  82. BOOL _highlighted;
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////////////////
  85. #pragma mark - NSObject
  86. - (id)initWithFrame:(CGRect)frame {
  87. if ((self = [super initWithFrame:frame])) {
  88. _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
  89. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  90. [self addSubview:_backgroundView];
  91. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  92. _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  93. [self addSubview:_contentView];
  94. _menuGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(menuGesture:)];
  95. }
  96. return self;
  97. }
  98. - (id)initWithCoder:(NSCoder *)aDecoder {
  99. if ((self = [super initWithCoder:aDecoder])) {
  100. if (self.subviews.count > 0) {
  101. _contentView = self.subviews[0];
  102. }else {
  103. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  104. _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  105. [self addSubview:_contentView];
  106. }
  107. _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
  108. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  109. [self insertSubview:_backgroundView belowSubview:_contentView];
  110. _menuGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(menuGesture:)];
  111. }
  112. return self;
  113. }
  114. ///////////////////////////////////////////////////////////////////////////////////////////
  115. #pragma mark - Public
  116. - (void)prepareForReuse {
  117. self.layoutAttributes = nil;
  118. self.selected = NO;
  119. self.highlighted = NO;
  120. self.accessibilityTraits = UIAccessibilityTraitNone;
  121. }
  122. // Selection highlights underlying contents
  123. - (void)setSelected:(BOOL)selected {
  124. _collectionCellFlags.selected = (unsigned int)selected;
  125. self.accessibilityTraits = selected ? UIAccessibilityTraitSelected : UIAccessibilityTraitNone;
  126. [self updateBackgroundView:selected];
  127. }
  128. // Cell highlighting only highlights the cell itself
  129. - (void)setHighlighted:(BOOL)highlighted {
  130. _collectionCellFlags.highlighted = (unsigned int)highlighted;
  131. [self updateBackgroundView:highlighted];
  132. }
  133. - (void)updateBackgroundView:(BOOL)highlight {
  134. _selectedBackgroundView.alpha = highlight ? 1.0f : 0.0f;
  135. [self setHighlighted:highlight forViews:self.contentView.subviews];
  136. }
  137. - (void)setHighlighted:(BOOL)highlighted forViews:(id)subviews {
  138. for (id view in subviews) {
  139. // Ignore the events if view wants to
  140. if (!((UIView *)view).isUserInteractionEnabled &&
  141. [view respondsToSelector:@selector(setHighlighted:)] &&
  142. ![view isKindOfClass:UIControl.class]) {
  143. [view setHighlighted:highlighted];
  144. [self setHighlighted:highlighted forViews:[view subviews]];
  145. }
  146. }
  147. }
  148. - (void)menuGesture:(UILongPressGestureRecognizer *)recognizer {
  149. NSLog(@"Not yet implemented: %@", NSStringFromSelector(_cmd));
  150. }
  151. - (void)setBackgroundView:(UIView *)backgroundView {
  152. if (_backgroundView != backgroundView) {
  153. [_backgroundView removeFromSuperview];
  154. _backgroundView = backgroundView;
  155. _backgroundView.frame = self.bounds;
  156. _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  157. [self insertSubview:_backgroundView atIndex:0];
  158. }
  159. }
  160. - (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView {
  161. if (_selectedBackgroundView != selectedBackgroundView) {
  162. [_selectedBackgroundView removeFromSuperview];
  163. _selectedBackgroundView = selectedBackgroundView;
  164. _selectedBackgroundView.frame = self.bounds;
  165. _selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  166. _selectedBackgroundView.alpha = self.selected ? 1.0f : 0.0f;
  167. if (_backgroundView) {
  168. [self insertSubview:_selectedBackgroundView aboveSubview:_backgroundView];
  169. }
  170. else {
  171. [self insertSubview:_selectedBackgroundView atIndex:0];
  172. }
  173. }
  174. }
  175. - (BOOL)isSelected {
  176. return _collectionCellFlags.selected;
  177. }
  178. - (BOOL)isHighlighted {
  179. return _collectionCellFlags.highlighted;
  180. }
  181. - (void)performSelectionSegue {
  182. /*
  183. Currently there's no "official" way to trigger a storyboard segue
  184. using UIStoryboardSegueTemplate, so we're doing it in a semi-legal way.
  185. */
  186. SEL selector = NSSelectorFromString([NSString stringWithFormat:@"per%@", @"form:"]);
  187. if ([self->_selectionSegueTemplate respondsToSelector:selector]) {
  188. #pragma clang diagnostic push
  189. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  190. [self->_selectionSegueTemplate performSelector:selector withObject:self];
  191. #pragma clang diagnostic pop
  192. }
  193. }
  194. ///////////////////////////////////////////////////////////////////////////////////////////
  195. #pragma mark - PSTCollection/UICollection interoperability
  196. #ifdef kPSUIInteroperabilityEnabled
  197. #import <objc/runtime.h>
  198. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
  199. NSMethodSignature *sig = [super methodSignatureForSelector:selector];
  200. if(!sig) {
  201. NSString *selString = NSStringFromSelector(selector);
  202. if ([selString hasPrefix:@"_"]) {
  203. SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
  204. sig = [super methodSignatureForSelector:cleanedSelector];
  205. }
  206. }
  207. return sig;
  208. }
  209. - (void)forwardInvocation:(NSInvocation *)inv {
  210. NSString *selString = NSStringFromSelector([inv selector]);
  211. if ([selString hasPrefix:@"_"]) {
  212. SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
  213. if ([self respondsToSelector:cleanedSelector]) {
  214. inv.selector = cleanedSelector;
  215. [inv invokeWithTarget:self];
  216. }
  217. }else {
  218. [super forwardInvocation:inv];
  219. }
  220. }
  221. #endif
  222. @end