Skip to content

Commit 337711b

Browse files
committed
[BUGFIX lxcid#80] Clean up gestures when collectionView property is nil'ed or layout is deallocated.
1 parent 9197b44 commit 337711b

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

LXRCVFL Example using Storyboard/LXRCVFL Example using Storyboard/LXCollectionViewController.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#import "LXCollectionViewController.h"
1010
#import "PlayingCard.h"
1111
#import "PlayingCardCell.h"
12-
#import "LXReorderableCollectionViewFlowLayout.h"
1312

1413
// LX_LIMITED_MOVEMENT:
1514
// 0 = Any card can move anywhere
@@ -21,8 +20,6 @@ @implementation LXCollectionViewController
2120

2221
- (void)viewDidLoad {
2322
[super viewDidLoad];
24-
// self.collectionView.collectionViewLayout = [LXReorderableCollectionViewFlowLayout new];
25-
// self.collectionView.collectionViewLayout = [LXReorderableCollectionViewFlowLayout new];
2623

2724
self.deck = [self constructsDeck];
2825
}

LXReorderableCollectionViewFlowLayout/LXReorderableCollectionViewFlowLayout.m

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ - (void)setupCollectionView {
107107
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationWillResignActive:) name: UIApplicationWillResignActiveNotification object:nil];
108108
}
109109

110+
- (void)tearDownCollectionView {
111+
// Tear down long press gesture
112+
if (_longPressGestureRecognizer) {
113+
UIView *view = _longPressGestureRecognizer.view;
114+
if (view) {
115+
[view removeGestureRecognizer:_longPressGestureRecognizer];
116+
}
117+
_longPressGestureRecognizer.delegate = nil;
118+
_longPressGestureRecognizer = nil;
119+
}
120+
121+
// Tear down pan gesture
122+
if (_panGestureRecognizer) {
123+
UIView *view = _panGestureRecognizer.view;
124+
if (view) {
125+
[view removeGestureRecognizer:_panGestureRecognizer];
126+
}
127+
_panGestureRecognizer.delegate = nil;
128+
_panGestureRecognizer = nil;
129+
}
130+
131+
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
132+
}
133+
110134
- (id)init {
111135
self = [super init];
112136
if (self) {
@@ -126,13 +150,9 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
126150
}
127151

128152
- (void)dealloc {
129-
130-
[self.panGestureRecognizer.view removeGestureRecognizer:self.panGestureRecognizer];
131-
[self.longPressGestureRecognizer.view removeGestureRecognizer:self.longPressGestureRecognizer];
132-
133153
[self invalidatesScrollTimer];
154+
[self tearDownCollectionView];
134155
[self removeObserver:self forKeyPath:kLXCollectionViewKeyPath];
135-
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
136156
}
137157

138158
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
@@ -494,6 +514,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
494514
[self setupCollectionView];
495515
} else {
496516
[self invalidatesScrollTimer];
517+
[self tearDownCollectionView];
497518
}
498519
}
499520
}

0 commit comments

Comments
 (0)