PSTCollectionViewCell.m 9.1 KB

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