Skip to content

Commit f1870c7

Browse files
committed
- Keep in sync to the issue reported in lxcid#16: Although I'm not very sure about the issue, it is pretty good practice to be conscious about the retaining of self and checking against it.
1 parent 84506db commit f1870c7

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

LXReorderableCollectionViewFlowLayout/LXReorderableCollectionViewFlowLayout.m

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ - (void)invalidateLayoutIfNecessary {
142142

143143
[self.dataSource collectionView:self.collectionView itemAtIndexPath:previousIndexPath willMoveToIndexPath:newIndexPath];
144144

145+
__weak typeof(self) weakSelf = self;
145146
[self.collectionView performBatchUpdates:^{
146-
[self.collectionView deleteItemsAtIndexPaths:@[ previousIndexPath ]];
147-
[self.collectionView insertItemsAtIndexPaths:@[ newIndexPath ]];
147+
__strong typeof(self) strongSelf = weakSelf;
148+
if (strongSelf) {
149+
[strongSelf.collectionView deleteItemsAtIndexPaths:@[ previousIndexPath ]];
150+
[strongSelf.collectionView insertItemsAtIndexPaths:@[ newIndexPath ]];
151+
}
148152
} completion:nil];
149153
}
150154

@@ -274,20 +278,27 @@ - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
274278

275279
self.currentViewCenter = self.currentView.center;
276280

281+
__weak typeof(self) weakSelf = self;
277282
[UIView
278283
animateWithDuration:0.3
279284
delay:0.0
280285
options:UIViewAnimationOptionBeginFromCurrentState
281286
animations:^{
282-
self.currentView.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
283-
highlightedImageView.alpha = 0.0f;
284-
imageView.alpha = 1.0f;
287+
__strong typeof(self) strongSelf = weakSelf;
288+
if (strongSelf) {
289+
strongSelf.currentView.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
290+
highlightedImageView.alpha = 0.0f;
291+
imageView.alpha = 1.0f;
292+
}
285293
}
286294
completion:^(BOOL finished) {
287-
[highlightedImageView removeFromSuperview];
288-
289-
if ([self.delegate respondsToSelector:@selector(collectionView:didBeginDraggingItemAtIndexPath:)]) {
290-
[self.delegate collectionView:self.collectionView didBeginDraggingItemAtIndexPath:self.selectedItemIndexPath];
295+
__strong typeof(self) strongSelf = weakSelf;
296+
if (strongSelf) {
297+
[highlightedImageView removeFromSuperview];
298+
299+
if ([strongSelf.delegate respondsToSelector:@selector(collectionView:didBeginDraggingItemAtIndexPath:)]) {
300+
[strongSelf.delegate collectionView:strongSelf.collectionView didBeginDraggingItemAtIndexPath:strongSelf.selectedItemIndexPath];
301+
}
291302
}
292303
}];
293304

@@ -306,21 +317,28 @@ - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
306317

307318
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:currentIndexPath];
308319

320+
__weak typeof(self) weakSelf = self;
309321
[UIView
310322
animateWithDuration:0.3
311323
delay:0.0
312324
options:UIViewAnimationOptionBeginFromCurrentState
313325
animations:^{
314-
self.currentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
315-
self.currentView.center = layoutAttributes.center;
326+
__strong typeof(self) strongSelf = weakSelf;
327+
if (strongSelf) {
328+
strongSelf.currentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
329+
strongSelf.currentView.center = layoutAttributes.center;
330+
}
316331
}
317332
completion:^(BOOL finished) {
318-
[self.currentView removeFromSuperview];
319-
self.currentView = nil;
320-
[self invalidateLayout];
321-
322-
if ([self.delegate respondsToSelector:@selector(collectionView:didEndDraggingItemAtIndexPath:)]) {
323-
[self.delegate collectionView:self.collectionView didEndDraggingItemAtIndexPath:currentIndexPath];
333+
__strong typeof(self) strongSelf = weakSelf;
334+
if (strongSelf) {
335+
[strongSelf.currentView removeFromSuperview];
336+
strongSelf.currentView = nil;
337+
[strongSelf invalidateLayout];
338+
339+
if ([strongSelf.delegate respondsToSelector:@selector(collectionView:didEndDraggingItemAtIndexPath:)]) {
340+
[strongSelf.delegate collectionView:strongSelf.collectionView didEndDraggingItemAtIndexPath:currentIndexPath];
341+
}
324342
}
325343
}];
326344
}

0 commit comments

Comments
 (0)