diff --git a/DraggableCollectionView/Helpers/LSCollectionViewHelper.m b/DraggableCollectionView/Helpers/LSCollectionViewHelper.m index 77929f6..0de110e 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); @@ -58,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:)]; @@ -73,11 +73,20 @@ - (id)initWithCollectionView:(UICollectionView *)collectionView } } + [_collectionView addGestureRecognizer:_longPressGestureRecognizer]; + [self layoutChanged]; } return self; } +- (void)dealloc +{ + [_collectionView removeObserver:self + forKeyPath:kObservingCollectionViewLayoutKeyPath + context:&kObservingCollectionViewLayoutContext]; +} + - (LSCollectionViewLayoutHelper *)layoutHelper { return [(id )self.collectionView.collectionViewLayout layoutHelper]; 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;