Skip to content

Commit

Permalink
Fixed various retain cyles issues and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveFortune committed Nov 18, 2013
1 parent 01cb95a commit 1ea421b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 46 deletions.
10 changes: 5 additions & 5 deletions I3DragBetweenHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

@interface I3DragBetweenHelper : NSObject

@property (nonatomic, readonly) BOOL isDragging;
@property (atomic, readonly) BOOL isDragging;

@property (nonatomic, readonly, retain) UIPanGestureRecognizer* currentGestureRecognizer;

Expand Down Expand Up @@ -194,19 +194,19 @@

/** The view that will contain the draggingView whilst dragging */

@property (nonatomic, retain) UIView* superview;
@property (nonatomic, weak) UIView* superview;

/** Must be an instance of UITableView or UICollectionView */

@property (nonatomic, retain) UIView* srcView;
@property (nonatomic, weak) UIView* srcView;

/** Must be an instance of UITableView or UICollectionView */

@property (nonatomic, retain) UIView* dstView;
@property (nonatomic, weak) UIView* dstView;

/** Delegate object for the drag routing */

@property (nonatomic, retain) NSObject<I3DragBetweenDelegate>* delegate;
@property (nonatomic, weak) NSObject<I3DragBetweenDelegate>* delegate;



Expand Down
98 changes: 58 additions & 40 deletions I3DragBetweenHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,18 @@ -(BOOL) startDragFromView:(UIView*) container atPoint:(CGPoint) point makeCopy:(
NSIndexPath* index = [self determineIndexForContainer:container
atPoint:point
forCell:&cell];
BOOL isDraggable = (BOOL)cell;

BOOL isDraggable = index != nil;

NSLog(@"Dragging at item:%d section:%d", [index item], [index section]);

/* Check in the delegate whether its draggable */

if(self.delegate && [self.delegate respondsToSelector:@selector(isCellAtIndexPathDraggable:inContainer:)]){

NSLog(@"Draggable %@ from delegate? %@", container, [self.delegate isCellAtIndexPathDraggable:index
inContainer:container] ? @"YES" : @"NO");
isDraggable = isDraggable && [self.delegate isCellAtIndexPathDraggable:index
inContainer:container];
inContainer:container];
}

if(!isDraggable){
Expand All @@ -205,27 +208,32 @@ -(BOOL) startDragFromView:(UIView*) container atPoint:(CGPoint) point makeCopy:(
cellPoint.x += containerFrame.origin.x;
cellPoint.y += containerFrame.origin.x;

if(makeCopy && [container isKindOfClass:[UITableView class]]){
if(makeCopy){

/* This is a bit hacky. Consider using KV coding to copy all the properties
of the CollectionView cell into the temporary dragging cell dynamically */

NSObject<UITableViewDataSource>* dataSource = [(UITableView*)container dataSource];
UIView* cellCopy;

if([container isKindOfClass:[UICollectionView class]]){

if(dataSource && [dataSource respondsToSelector:@selector(tableView:cellForRowAtIndexPath:)]){
UICollectionViewCell* cell = [(UICollectionView*)container cellForItemAtIndexPath:index];
[cell setHighlighted:NO];
NSData* viewCopyData = [NSKeyedArchiver archivedDataWithRootObject:cell];
cellCopy = [NSKeyedUnarchiver unarchiveObjectWithData:viewCopyData];

self.draggingView = [dataSource tableView:(UITableView*)container
cellForRowAtIndexPath:index];
}
}
else if(makeCopy && [container isKindOfClass:[UICollectionView class]]){

NSObject<UICollectionViewDataSource>* dataSource = [(UICollectionView*)container dataSource];
else if([container isKindOfClass:[UITableView class]]){

if(dataSource && [dataSource respondsToSelector:@selector(collectionView:cellForItemAtIndexPath:)]){

self.draggingView = [dataSource collectionView:(UICollectionView*)container
cellForItemAtIndexPath:index];
UITableViewCell* cell = [(UITableView*)container cellForRowAtIndexPath:index];
[cell setHighlighted:NO];
NSData* viewCopyData = [NSKeyedArchiver archivedDataWithRootObject:cell];
cellCopy = [NSKeyedUnarchiver unarchiveObjectWithData:viewCopyData];

}

self.draggingView = cellCopy;


}
else{
self.draggingView = cell;
Expand All @@ -238,8 +246,9 @@ -(BOOL) startDragFromView:(UIView*) container atPoint:(CGPoint) point makeCopy:(

self.draggingView.frame = [self.superview convertRect:cellFrame fromView:container];


NSLog(@"Adding dragging data: %d", [index row]);
[self.draggingView setHidden:NO];
NSLog(@"Adding dragging data: %d, draggingView %@", [index row], self.draggingView);
NSLog(@"Dragging view opactiy %f, is hidden", self.draggingView.alpha);

return YES;

Expand Down Expand Up @@ -426,7 +435,6 @@ -(void) handlePan:(UIPanGestureRecognizer*) gestureRecognizer{

-(void) handleDragStarted:(UIPanGestureRecognizer*) gestureRecognizer{


CGPoint pointInDst = [gestureRecognizer locationInView:self.dstView];
CGPoint pointInSrc = [gestureRecognizer locationInView:self.srcView];
self.isDragging = YES;
Expand Down Expand Up @@ -561,7 +569,9 @@ -(void) handleDragStartedInSrcAtPoint:(CGPoint) point{

/* If its an invalid cell then no dragging is started */

self.isDragging = NO;
//self.isDragging = NO;
self.draggingView = nil;

}

}
Expand Down Expand Up @@ -599,19 +609,22 @@ -(void) handleDragFromSrcStoppedInSrcAtPoint:(CGPoint) point{
atPoint:point
forCell:&cell];

BOOL isExchangable = (BOOL)cell;
BOOL isExchangable = YES;//(BOOL)cell;


/* Check in the delegate whether its exchangable */

if(self.delegate && [self.delegate respondsToSelector:@selector(isCellInSrcAtIndexPathExchangable:withCellAtIndexPath:)]){

isExchangable = isExchangable && [self.delegate isCellInSrcAtIndexPathExchangable:index
isExchangable = /*isExchangable &&*/ [self.delegate isCellInSrcAtIndexPathExchangable:index
withCellAtIndexPath:self.draggingIndexPath];
}

if(!isExchangable){
NSLog(@"Invalid Cell or not Exchangable.");

[self handleDragFromSrcStoppedAtPoint:[self.superview convertPoint:point fromView:self.dstView]];

return;
}

Expand Down Expand Up @@ -647,8 +660,8 @@ -(void) handleDragFromSrcStoppedInSrcAtPoint:(CGPoint) point{
}

[self changeSuperviewForView:self.draggingView forSuperview:nil];
//[self reloadDataInView:self.dstView atIndeces:@[index, self.draggingIndexPath]];
[self reloadDataInView:self.srcView];
[self reloadDataInView:self.dstView atIndeces:@[index, self.draggingIndexPath]];
//[self reloadDataInView:self.srcView];
[self handleDragAnimationComplete];

}];
Expand Down Expand Up @@ -728,7 +741,7 @@ -(void) handleDragStartedInDstAtPoint:(CGPoint) point{

/* If its an invalid cell then no dragging is started */

self.isDragging = NO;
//self.isDragging = NO;
self.draggingView = nil;
}

Expand Down Expand Up @@ -812,14 +825,14 @@ -(void) handleDragFromDstStoppedInDstAtPoint:(CGPoint) point{
atPoint:point
forCell:&cell];

BOOL isExchangable = (BOOL)cell;
BOOL isExchangable = YES;//(BOOL)cell;


/* Check in the delegate whether its exchangable */

if(self.delegate && [self.delegate respondsToSelector:@selector(isCellInDstAtIndexPathExchangable:withCellAtIndexPath:)]){

isExchangable = isExchangable && [self.delegate isCellInDstAtIndexPathExchangable:index
isExchangable = /*isExchangable &&*/ [self.delegate isCellInDstAtIndexPathExchangable:index
withCellAtIndexPath:self.draggingIndexPath];
}

Expand Down Expand Up @@ -986,38 +999,43 @@ -(void) transitionView:(UIView*) view
NSLog(@"Animation complete!");


[self changeSuperviewForView:view
forSuperview:superview];


[self handleDragAnimationComplete];


if(isFromSrc && self.delegate &&
[self.delegate respondsToSelector:@selector(dragFromSrcSnappedBack:)]){


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.delegate performSelector:@selector(dragFromSrcSnappedBack:) withObject:view];
#pragma clang diagnostic pop

}
else if(!isFromSrc && self.delegate &&
[self.delegate respondsToSelector:@selector(dragFromDstSnappedBack:)]){

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.delegate performSelector:@selector(dragFromDstSnappedBack:) withObject:view];
#pragma clang diagnostic pop

}
else{
NSLog(@"Selector not valid");
NSLog(@"Selector not valid, from Src %@, delegate %@, responds %@",
isFromSrc ? @"YES" : @"NO",
self.delegate,
[self.delegate respondsToSelector:@selector(dragFromSrcSnappedBack:)] ? @"YES" : @"NO");
}


[self changeSuperviewForView:view
forSuperview:superview];



CGRect localFrame = [superview convertRect:frame fromView:self.superview];
view.frame = localFrame;


[self handleDragAnimationComplete];

};

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
Objective-C helper class(es) for the iOS platform that handle drag and drop logic between 2 UITableView s and/or UICollectionView s
iOS Drag-n-Drop Between Helper
------------------------------

Objective-C helper class(es) for the iOS platform that handle drag and drop logic between 2 UITableView s and/or UICollectionView s.

Requires ARC to compile.

0 comments on commit 1ea421b

Please sign in to comment.