@@ -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