Giter Site home page Giter Site logo

lukescott / draggablecollectionview Goto Github PK

View Code? Open in Web Editor NEW
550.0 550.0 175.0 364 KB

Extension for the UICollectionView and UICollectionViewLayout that allows a user to move items with drag and drop. --- HELP WANTED --- Looking for maintainer and help with the experimental branch.

License: MIT License

Objective-C 100.00%

draggablecollectionview's People

Contributors

lukescott avatar ndlupine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

draggablecollectionview's Issues

Issues in iOS7 Related to Drag & drop for one rows between the sections

Issues

1: When i am dragging a cell from second section to first section at that time cell is not insert at the last cell of first section.
2: when i am dragging a cell from one section to other section and return back to previous one at that time its overlap with other cell and also the cause of exception.

3: when i am dragging a cell from one section to an empty section at that time i am not able to understand at wich indexpath it is inserted.

Current indexpaths while warping cells

Hi Luke,

i'm having the issue that, while dragging cells around their indexpath is not updated.
My goal is to prevent dragging cells inbetween certain other cells therefore i check what kind of cell is underneath the current index, the index before and the index after.
Unfortunately this doesn't work anymore once the cells start warping.

Do you have any suggestion on where to start?

Thanks in advance!

Prevent Cells from Reorganizing Themselves

I'm trying to alter the LSCollectionViewLayoutHelper file to allow me to simply exchange position of the cells without altering the position's of the other cells. For example, I drag a cell from the 3rd row 2nd column, to the 2nd row 2nd column, then instead of switching position's, the cell in the 2nd row 2nd column gets pushed down to the 3rd row 1st column.

How would I be able to prevent this? Any help would be greatly appreciated!

Is this a typo in LSCollectionViewHelper.m ?

Hello,

I'm just playing around with this and noticed what I think is an typo on line 236 of LSCollectionViewHelper.m . This is where the mockcell is created after the long press.
Should line 236 not be [cell removeFromSuperview] instead of
[mockCell removeFromSuperview] since mockCell does not yet exist ?

234           UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
235            cell.highlighted = NO;
236            [mockCell removeFromSuperview];
237            mockCell = [[UIImageView alloc] initWithFrame:cell.frame];

Folder support

Need to add folder support after adding "closest index tracking".

The cell is being dragged should be set to "selected" status if possible.

I have a collection view which can drag and drop cells based on this library, and my cell can be single-selected(like cell border's color change). If I have selected cell A, and then long press cell B to trigger dragging, the selected status can not move from cell A to cell B.
In order to resolve this issue, I put some codes like below into LSCollectionViewHelper.m - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)sender:

        NSArray* selectedIndexPathes = self.collectionView.indexPathsForSelectedItems;
        for (NSIndexPath* p in selectedIndexPathes)
        {
            [self.collectionView deselectItemAtIndexPath:p animated:NO];
        }
        [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

Multiple selection drag and drop

I have an implementation of a multiple item grid selection drag and drop (i.e. with an Edit mode where you tap on multiple items and then initiate the drag) that I've done with a framework for iOS5 and am in the process of converting it to work with UICollectionViews.

I'm tempted to extend DraggableCollectionView to handle this, but am wondering if you've had any thoughts about this? It seems like the biggest barrier is a portion would have to be handled in the data source, in that you would actually be changing the count of the number of items in the set during the drag (i.e. remove all but one of the selected items from the set, leaving the last one as the hidden element).

Anyway, just curious if this is something you've thought about and whether you'd be interested in seeing if we could make DraggableCollectionView handle this?

Mock cell generated in long press event can not support custom UICollectionView class.

Below codes part is form the hanleLongPressGesture: methods in LSCollectionViewHelper.m file:

        // Create mock cell to drag around
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        cell.highlighted = NO;
        [mockCell removeFromSuperview];
        mockCell = [[UIImageView alloc] initWithFrame:cell.frame];
        mockCell.image = [self imageFromCell:cell];
        mockCenter = mockCell.center;
        [self.collectionView addSubview:mockCell];
        [UIView
         animateWithDuration:0.3
         animations:^{
             mockCell.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
         }
         completion:nil];

If I use a custom UICollectionView class, how can I make the same effect in demo app(like UICollectionViewCell object turns dark once be long pressed, and turns bigger(110%) than original size during the drag operation? (By testing custom cell can't have the same effect)

Thanks.

Problem dragging in flowlayout when only 1 section

It took me a while to find the problem here.

If there's only 1 section, there's an additional, unwanted animation when dragging a cell to a new position, when you let go of the object. Every object which moves as part of the change actually animates into it's new position, behind a copy of itself. It's difficult to explain, but if you start the flowlayout demo, and change SECTION_COUNT to 1, and then drag some cells around, you'll hopefully see what i mean.

Selected cell hides selectedBackgroundView on dragging of neighbor cell

Hi,

If I don't miss anything in my implementation then I found a bug. If I select the first cell and then hold a finger on the second cell, the second cell gets selected for some time (e.g. shows my customized selectedBackgroundView), then longtap recognizer triggers and selection state gets cancelled, the first cell then loses selected state as well, but it's only a graphical defect, in fact If I start scrolling collection view the first cell repaints and becomes visually selected as before. I use simple horizontal flow layout inherited from DraggableCollectionViewFlowLayout.

Exception from layoutAttributesForItemAtIndexPath

I have a collection view with 4 sections — everything except for the second section is static, and cannot be dragged.

I'm seeing an out of bounds exception when trying to drag any cell (on long press), which is bringing everything to a screeching halt.

What can I provide (short of the whole project) to help debug this?

Need help

Hi,
thx for your work. but i dont't get it run :(

I have a collection view with only one section, so my datasource is an array with "pages" objects. when a drag a cell to a new position, the animation is ok and the other cells are moving corractly. when i drop the cell, the cell that is now on the position of the dragged cell, returns to the content of the dragged cell.

in short: if i have two cells wih different content, after dragging both are with equal content.

what do i wrong?

thx
thorsten

Cocoapods

Someone submitted a fork of your project to cocoapods. It seems to be out of date or buggy to me, and I struggled for a while with it before realizing that it was a fork. He submitted it under the name "DraggableCollectionView". Normally I submit forks under DraggableCollectionView@kylerobson or similar. I consider his method borderline dishonest and at least misleading. If you would submit your project as a cocoapod and ideally get his removed or renamed, that would be ideal. For now I use your project as a private cocoapod.

Thanks,
Kyle

Crash when using adding section headers

I am running the FlowLayoutDemo, but when I try adding section headers and pick up a cell to move, I get a crash as soon as I release the item or drag it over another.

2013-07-22 12:14:04.596 FlowLayoutDemo[99484:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: <_UICollectionViewItemKey: 0xdc82d70> Type = SV Kind = UICollectionElementKindSectionHeader IndexPath = <NSIndexPath 0xdc82c20> 2 indexes [0, 0])'

how to swab two cells without reordering?

Hello,
how could I swab two cells without reordering the other cells
for example:
I have the cells below:
123
456
789

After swapping cell no.6 with cell no.9 it should become
123
459
786

Handle different sized items

First thanks for writing this class, I'm having fun trying it out. And very possibly this isn't a bug, just a user error. I'm trying to user DragableCollectionView with items of different sizes. I've done this by overriding this method in my collection view controller:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout  *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return CGSizeMake(320, 88);
        default:
            return CGSizeMake(320, 44);
    }
}

So my first cell is 88 everything else is 44. (I realize hardcoding this way won't work generally, but I think it is simplest way to reproduce bug...)

The problem is that this setup messes up the drag in progress animations. For example if I drag line 1 (small one) up above line 0 (the big one) then when the big line is animated down to fill the position of the small line... it loses it's big size.

It seems that my sizeForItemAtIndexPath method is getting called as the drag happens, so it sorta makes sense what's happening. But I don't get the moveItemAtIndexPath callback until the drop is performed, so there's not really any way for me to know what the correct value to return is.

I hope I made some sense their, please let me know if you think this is a bug or not. And if so how it might be fixed. Thanks!

Dragging seems a bit slugish on device since closest index update

I didn't notice this is simulator, but the dragging seems a bit sluggish to me since 5094272 on a physical iPad. Maybe I'm just imagining it.

I think it might have to do with the "closest item" changing too frequently, causing the layout to be regenerated more often. I'll have to run some tests to confirm.

Maybe adding some sort of small delay in re-ordering would smooth things out a bit.

Crash/Bug when a second finger is held on the screen

Hi there,
I have found a crash and some unexpected behaviour which is caused by placing a second finger on the screen. It does not always result in a crash (as it seems to depend on what cell you have selected originally and the location of the second finger press). To cause the issue I begin selecting a cell and move it to a different location. Whilst still holding onto the cell I am moving i then place a second finger on the screen and hold it down. If I then release the original cell i was moving (leaving the second finger on the screen) it can cause a crash, move the cell to an undesired location, or work as normal. I am using an iPad 3 and using your demo project on the flow layout to create the issue as well as my own project. If you want any more info just ask!

Closest index tracking

Currently use indexPathForItemAtPoint: to determine where to move the cell. This requires an item to directly intersect another to move it. Need to make a new function to determine the closest index instead for more fluid response and greater flexibility.

Crash with two fingers & centroid issue

If you hold your finger on one cell, then place a finger on the other the center of the two fingers is used (different issue). When you lift the first finger it crashes with:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for number of items before section 2147483647 when there are only 5 sections in the collection view'

I suspect it might have something to do with a numberOfItemsInSection call somewhere, but I'm going to have to do some more debugging to find out.

reloadData issue

This is a very weird issue.
It works fine in simulator, but not do the same in real iPhone.

I used [collectionView reloadData]; in didMoveItemAtIndexPath, but just the only cell which I moved refreshed. others cell didn't refresh.

please help me, thanks!

(README) Help Wanted

As many of you are aware I have not been very active lately. The reason for this is I am involved with a major project for work that takes up most of my time. What's left over I used to spend on side projects. I've come to realize that I need to spend more time with family, and I've been doing that. When the project is done I'll have more energy to spend on projects like this.

I'll be spending what time I can resolving as many of the outstanding issues that I can. Not all issues can be resolved cleanly, given how the extension works.

I have an experimental (and incomplete) version in the experimental branch. Instead of trying to subclass a layout and reorder the visible cells it does the re-ordering at layout calculation instead of post-calculation. This solves the problem of having differing cell sizes. Unfortunately this means that I've had to re-implement Apple's Flow Layout from scratch. Other layouts will have to support it directly in their layout calculations.

If anyone is interested in continuing my experimental work, you are more than welcome to do so. Let me know! I won't be able to get to it for a 2-3 months at least. The flow layout itself is incomplete. There are also some iOS 7 optimizations with layout recalculations that need to be take into account (context). The goal is to have it be 1-to-1 with Apple's implementation, plus the ability to specify alignment of the rows (left, center, right, justified - Apple has this functionality in their internal class, but they have made it private).

How to subclass UICollectionViewLayoutAttributes and use the custom attributes in your extension?

Hi Luke, it's cool to provide such a great extension, thank you!

I successfully implemented it in my project work. In the same way I've tried to add a jiggling effect and a delete button on each cell when the longPress is fired (exactly the same behavior of iPhone's native Springboard).

To do so I've introduced a trigger (isDeletionModeActive) in viewController.m and it is positioned to YES when longPress gesture began.
Then I've subclassed the 2 classes below, for 2 reasons:

  • UICollectionViewLayoutAttributes: for adding a new custom attribute (deletionButtonHidden) triggered via the isDeletionModeActive status the viewController.m
  • UICollectionViewCell: for drawing of delete button and starting a jiggling effect depending on layoutAttributes.isDeleteButtonHidden value
    (you can find more details of the logic here : http://code.tutsplus.com/tutorials/uicollectionview-layouts--mobile-13426)

But the resulting behavior isn't the one I expect as the dragging works (but not as smooth as expected during pan gesture) but the destination cell disappears when I dropped the selected cell on it.

Do you a solution so that I can subclass UICollectionViewLayoutAttributes without affect your extension logic?

Thanks a lot for your help.

Centroid issue

When you hold your finger over a cell and start dragging, then place a second finger on the screen it uses the center position between both fingers rather than the position of the first finger.

Need to adjust the gesture to only use one finger in some way.

How to resize when dragging.

@lukescott Please help...

My Requirement:

My requirement is to achieve, what is displayed in the following image.
img_1

I am able to modify the UICollectionView appearance. But, when I drag the big cell to position of small cell the big cell size changes to small or verse versa. Which I don't want to happen.
img_2

cannot scroll while dragging down

I have lots of cells out side of the screen, and while dragging, when I drag one cell to the bottom of the screen, the UICollectionView cannot scroll slowly to show more cells.

However, the demo can achieve so. I am not sure what I missed?

Cell does not move outside the CollectionView

Hi there,
it's not a real issue, but I'd like to know if it is possible to have.

I have a ViewController with UICollectionView (DraggableCollectionView) and a UILabel below. Is it possible to drag a Cell outside the collection view and detect when it collides with UILabel ?
Currently, Cell goes below the UICollectionView border so it cant go out the collection view.

Rgds,
edcruise.

Cannot get Flow Layout working under iOS 8

I tried to use your DraggableCollectionView for one of my projects, where we wanted to implement an item reorder feature. Your demo looks amazing, the live-reordering of cells and the automatic scrolling at the edges. Unfortunately, I cannot get it to work in my project. I refactored the Storyboard/Code to use the same structure as in your FlowLayout example. A VC with a CollectionView, conforming to the UICollectionViewDataSource_Draggable protocol (and implementing canMove and moveItem...) and the datasource and delegate of the CV are also set in the storyboard. The layout of the CV is set to the DraggableCollectionViewLayout. But nothing happens when I longPress on a cell. Did I miss something? Is it an issue on iOS 8?
Thank you so much for the amazing work

Can't compliant with CoreData + NSFetchedResultsController?

Hi, luke,
Thanks for your code. I get a problem when use CoreData + NSFetchedResultsController as data source instead of NSArray. I use the code:https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController to make UICollectionView works with NSFetchedResultsController just like UITableView together with NSFetchedResultsController.
2014-07-14 7 24 48
2014-07-14 7 53 30

in NSFetchedResultsController, I use alphabeticPart and orderPart to sort cell, and use alphabeticPart as sectionNameKeyPath, make all same with before, just I use orderPart to sort cell in the section.
2014-07-14 7 57 26
but I drag and drop cell in the same section, i.e move B3 to B2, it likes this. Only after I [self.collection reloadData], it looks right. And I move cell to another section, it crashes. This is maybe layout information error. Now I just want to work out with first problem:after move a cell in the same section, onsreen get the same cell.
2014-07-14 7 59 35

Memory keep growing

I really love this library,thank you!
I have a concern about one issue (I did see my app crashed because of it) . please help.
--here is how I reproduced it.--
I modified FlowLayoutDemo project and created a new UIViewController( and Converted to UINavigationController) in it.
I placed a single button from the new UIViewController to invoke (push segue) the Draggable UICollectionView, it will increase about 2MB every time when I click the push button.(then I click Back button & push it again ). It seems every time it will call - (LSCollectionViewHelper *)getHelper to init a new helper that keep the memory growing. All previous memory still retained by some reason .
Any idea about how to solve this issue?

Thanks
screen shot 2014-04-26 at 9 20 39 pm

Allen

Unmoveable objects support?

I'm going to make a request here which is maybe a little complicated but crucial to my usage.

The last item in section 0 is a button. The idea is, I want the user to be able to switch around all the other icons, but have them not able to put any icons after that particular one.

I know this is kind of difficult - the situation where it's mid-way through is probably impossible, because it has to move at some point. But would you be able to think of a reasonable way to implement a feature where I could make sure that the last object doesn't move?

DraggableCollectionView + NSFetchedResultsController

Anyone tried using the NSfetchedresultscontroller with it? Everytime I move stuff around, it behaves oddly, i.e things get duplicates , don't refresh etc.. Also noticed that the following gets called NSFetchedResultsChangeMove which causes the animation to look really wonky.

Won't Respond to Long Press in a Collection View Managed by a UIViewController

I have a collection view contained with in a main view managed by a UIViewController. This is necessary as there are other views within the main view. The collection view and it's cells work just fine. But, I can't get a response to a long press in a cell.

Should I add a long press gesture recognizer and redirect it somewhere? Or what?

Any help would be appreciated.

Carl Carlson

Section doesn't expand/retract when it should

When you move a cell to another section and it ends up creating another row that section's height doesn't increase (which should push the others down). The reverse is also true: If you move a cell away from a section and it removes a row, that section's height isn't reduced.

This happens while dragging though. When you let go it corrects itself. The animation was messed up when this happened, but 151958f fixed the animation problem.

One way to fix this is by changing the UICollectionViewLayoutAttribute frames. Essentially detect when a section changes size and add/subtract the X or Y position (depending on the scrolling direction). But this makes the assumption that the layout is a Flow layout... So this can't be implemented in LSCollectionViewLayoutHelper. It would have to be in DraggableCollectionViewFlowLayout, but that's a bit of a hack.

Any "fix" would probably have to be layout specific... It could probably be mitigated slightly by inserting an extra cell to the end of each section.

Paging Support

I would like to have a Paging Support for this awesome Addition.

I think it have to be implemented in the handlePanGesture: method with a "pagingEnabled" property.

Cells reordering does not work in iOs 10

Hello everybody, I've discovered an issue in xCode 8.0 beta 2 (iOs 10 sdks).
I can't drag the cells anymore, it think is something related to _panPressGestureRecognizer in LSCollectionViewHelper.m.
Someone could help to fix it?
Thanks.

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

get this error sometimes, when drag and dropping to item (can reproduce in Flowlayout Demo sometimes, cant figure out a way to consistenly make it happen yet, but i can get it to show up once per 4-5 tries). After some investigations, i think it occurs in

  • (void)warpToIndexPath:(NSIndexPath *)indexPath

in the performBatchUpdates ...

[self.collectionView performBatchUpdates:^{
    self.layoutHelper.hideIndexPath = indexPath;
    self.layoutHelper.toIndexPath = indexPath;
} completion:nil];

I dont know why though, any idea?

iOS9 it become unstable move when drag

I tested it under iOS9 beta / iphone6 Plus,
When I drag one item, neighboring items will move weirdly when I move dragged item to other place.
It seems compute the wrong coordinate in iOS9.0 beta.

Crashes on longPress with more than one section

This has been wonderfully useful, but when I added a second section, it crashes on a longPress in: - (NSIndexPath *)indexPathForItemClosestToPoint:(CGPoint)point

I suspect that I am missing something really basic in my implementation. A FlowLayoutDemo with sections would probably clear up my error.

Relevant Code in indexPathForItemClossestToPoint:

    NSInteger items = [self.collectionView numberOfItemsInSection:i];
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:items inSection:i];
    UICollectionViewLayoutAttributes *layoutAttr;
    CGFloat xd, yd;
    if (items > 0) {

CRASHES HERE-->>>>>> layoutAttr = [self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:nextIndexPath];

There are no are error messages in the debugger.

After longPressing in section 1, item 2:
items = 3,
nextIndexPath = 1,3 .
Perhaps the problem is because of zero based counting, the last valid indexPath is 1,2.

I would greatly appreciate some help here.

Thanks

Customizing cell sizes would crash the dragging

Simple to reproduce.

In the demo app, in ViewController.m, add the following method:

  • (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return CGSizeMake(indexPath.row == 0 ? 200 : 100, 100);
    }

See screenshot for the layout:

ios simulator screen shot may 22 2014 3 28 12 pm

Trying to drag any cell would crash with an exception.

Support multiple sections

I need to add support for multiple sections - that is dragging an item from one section to another. This shouldn't be too difficult.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.