Skip to content
This repository was archived by the owner on Oct 31, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions DraggableCollectionView/Helpers/LSCollectionViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <QuartzCore/QuartzCore.h>

static int kObservingCollectionViewLayoutContext;
static NSString* const kObservingCollectionViewLayoutKeyPath = @"collectionViewLayout";

#ifndef CGGEOMETRY__SUPPORT_H_
CG_INLINE CGPoint
Expand Down Expand Up @@ -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);
Expand All @@ -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:)];
Expand All @@ -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 <UICollectionViewLayout_Warpable>)self.collectionView.collectionViewLayout layoutHelper];
Expand Down
4 changes: 4 additions & 0 deletions DraggableCollectionView/UICollectionView+Draggable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions DraggableCollectionView/UICollectionView+Draggable.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down