From 2386b07fb4468e2752f40fd0aa853bca93b71b94 Mon Sep 17 00:00:00 2001 From: Vineet Shah Date: Sat, 25 Oct 2014 02:54:20 -0400 Subject: [PATCH 1/3] Remove collectionView KVO on dealloc --- .../Helpers/LSCollectionViewHelper.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/DraggableCollectionView/Helpers/LSCollectionViewHelper.m b/DraggableCollectionView/Helpers/LSCollectionViewHelper.m index 77929f6..b96a5e0 100644 --- a/DraggableCollectionView/Helpers/LSCollectionViewHelper.m +++ b/DraggableCollectionView/Helpers/LSCollectionViewHelper.m @@ -11,6 +11,7 @@ #import static int kObservingCollectionViewLayoutContext; +static NSString* const kObservingCollectionViewLayoutKeyPath = @"collectionViewLayout"; #ifndef CGGEOMETRY__SUPPORT_H_ CG_INLINE CGPoint @@ -49,7 +50,7 @@ - (id)initWithCollectionView:(UICollectionView *)collectionView if (self) { _collectionView = collectionView; [_collectionView addObserver:self - forKeyPath:@"collectionViewLayout" + forKeyPath:kObservingCollectionViewLayoutKeyPath options:0 context:&kObservingCollectionViewLayoutContext]; _scrollingEdgeInsets = UIEdgeInsetsMake(50.0f, 50.0f, 50.0f, 50.0f); @@ -78,6 +79,13 @@ - (id)initWithCollectionView:(UICollectionView *)collectionView return self; } +- (void)dealloc +{ + [_collectionView removeObserver:self + forKeyPath:kObservingCollectionViewLayoutKeyPath + context:&kObservingCollectionViewLayoutContext]; +} + - (LSCollectionViewLayoutHelper *)layoutHelper { return [(id )self.collectionView.collectionViewLayout layoutHelper]; From 70d43e902a45d2a68f9d1457ca1dda04deedbb69 Mon Sep 17 00:00:00 2001 From: Vineet Shah Date: Sat, 25 Oct 2014 02:56:30 -0400 Subject: [PATCH 2/3] Remove gesture recognizer's dependency on itself --- DraggableCollectionView/Helpers/LSCollectionViewHelper.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DraggableCollectionView/Helpers/LSCollectionViewHelper.m b/DraggableCollectionView/Helpers/LSCollectionViewHelper.m index b96a5e0..0de110e 100644 --- a/DraggableCollectionView/Helpers/LSCollectionViewHelper.m +++ b/DraggableCollectionView/Helpers/LSCollectionViewHelper.m @@ -59,7 +59,6 @@ - (id)initWithCollectionView:(UICollectionView *)collectionView _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; - [_collectionView addGestureRecognizer:_longPressGestureRecognizer]; _panPressGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; @@ -74,6 +73,8 @@ - (id)initWithCollectionView:(UICollectionView *)collectionView } } + [_collectionView addGestureRecognizer:_longPressGestureRecognizer]; + [self layoutChanged]; } return self; From 7d89ac04d7d00f80140b52c94b7ea81b01fdb00b Mon Sep 17 00:00:00 2001 From: Vineet Shah Date: Sat, 25 Oct 2014 02:59:15 -0400 Subject: [PATCH 3/3] Create public method to remove the associated object --- DraggableCollectionView/UICollectionView+Draggable.h | 4 ++++ DraggableCollectionView/UICollectionView+Draggable.m | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/DraggableCollectionView/UICollectionView+Draggable.h b/DraggableCollectionView/UICollectionView+Draggable.h index 2ac09cb..ca369b8 100644 --- a/DraggableCollectionView/UICollectionView+Draggable.h +++ b/DraggableCollectionView/UICollectionView+Draggable.h @@ -12,4 +12,8 @@ @property (nonatomic, assign) BOOL draggable; @property (nonatomic, assign) UIEdgeInsets scrollingEdgeInsets; @property (nonatomic, assign) CGFloat scrollingSpeed; + +// Call this method from the -dealloc method of any class that accesses the properties above +- (void)removeHelper; + @end diff --git a/DraggableCollectionView/UICollectionView+Draggable.m b/DraggableCollectionView/UICollectionView+Draggable.m index 735cf90..9ef9875 100644 --- a/DraggableCollectionView/UICollectionView+Draggable.m +++ b/DraggableCollectionView/UICollectionView+Draggable.m @@ -20,6 +20,14 @@ - (LSCollectionViewHelper *)getHelper return helper; } +- (void)removeHelper +{ + LSCollectionViewHelper *helper = objc_getAssociatedObject(self, "LSCollectionViewHelper"); + if (helper) { + objc_setAssociatedObject(self, "LSCollectionViewHelper", nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + } +} + - (BOOL)draggable { return [self getHelper].enabled;