Skip to content

Commit

Permalink
[BUGFIX #80] Clean up gestures when collectionView property is nil'ed…
Browse files Browse the repository at this point in the history
… or layout is deallocated.
  • Loading branch information
lxcid committed Feb 13, 2015
1 parent 9197b44 commit 337711b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Expand Up @@ -9,7 +9,6 @@
#import "LXCollectionViewController.h"
#import "PlayingCard.h"
#import "PlayingCardCell.h"
#import "LXReorderableCollectionViewFlowLayout.h"

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

- (void)viewDidLoad {
[super viewDidLoad];
// self.collectionView.collectionViewLayout = [LXReorderableCollectionViewFlowLayout new];
// self.collectionView.collectionViewLayout = [LXReorderableCollectionViewFlowLayout new];

self.deck = [self constructsDeck];
}
Expand Down
Expand Up @@ -107,6 +107,30 @@ - (void)setupCollectionView {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationWillResignActive:) name: UIApplicationWillResignActiveNotification object:nil];
}

- (void)tearDownCollectionView {
// Tear down long press gesture
if (_longPressGestureRecognizer) {
UIView *view = _longPressGestureRecognizer.view;
if (view) {
[view removeGestureRecognizer:_longPressGestureRecognizer];
}
_longPressGestureRecognizer.delegate = nil;
_longPressGestureRecognizer = nil;
}

// Tear down pan gesture
if (_panGestureRecognizer) {
UIView *view = _panGestureRecognizer.view;
if (view) {
[view removeGestureRecognizer:_panGestureRecognizer];
}
_panGestureRecognizer.delegate = nil;
_panGestureRecognizer = nil;
}

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}

- (id)init {
self = [super init];
if (self) {
Expand All @@ -126,13 +150,9 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
}

- (void)dealloc {

[self.panGestureRecognizer.view removeGestureRecognizer:self.panGestureRecognizer];
[self.longPressGestureRecognizer.view removeGestureRecognizer:self.longPressGestureRecognizer];

[self invalidatesScrollTimer];
[self tearDownCollectionView];
[self removeObserver:self forKeyPath:kLXCollectionViewKeyPath];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
Expand Down Expand Up @@ -494,6 +514,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[self setupCollectionView];
} else {
[self invalidatesScrollTimer];
[self tearDownCollectionView];
}
}
}
Expand Down

0 comments on commit 337711b

Please sign in to comment.