Welcome back again.
In this lesson we'll talk about drag and drop between collection views.
It's unfortunately a bit of a longer court sided and quite easier to try and get it done in in one setting.
So let's going to school and here I'm going to start a new application single view application drag
and drop in collections.
And this time unlike the last few times that I've been using a UI collection view controller I'm actually
going to use a UI collection view.
So to do that I'm going to go ahead in here and say I have an outlet saw a b outlet and that's going
to be a value event called my collection view.
She's off the type UI collection view.
So that's the thing that we usually do with the that I gander up.
Sometimes they prefer to just type with myself when I go for storyboard.
And in here I'm going to go ahead and add a collection view which is here.
So that's my collection view and I'm going to go ahead and say it's actually placed somewhere like this.
I'm going to add all the missing constraints and this one cell in here.
I'm going to call it to be a cell.
I just like the last few times.
I'm only going to worry about changing the color of a cell.
I'm not going to make an actual cell in here because that would just exponentially make our code longer.
I don't really want to do that.
So the next thing is I'm going to connect my connection view that this is my collection view and then
I'm going to go ahead and say in here my collection view with view controller it is the data source
and then it is also the delegate.
And I'm actually going to add two more things.
One of them is Dragon delegate and the other one is a drop delegate as you can imagine you can do all
of these things in coding as well.
Well that's right they're doing it in here because going to simplify my code than the amount of code
that they have to be I have to be writing.
We then have to keep something in mind when I select my collection of you.
Here it says user interaction is enable.
But that doesn't mean the drag interaction is also enabled and that's something that you have to do
manually.
So I'm going to head into the court in my view controller interview that Lord I'm going to go ahead
and say self dogs my collection view DOD drag interaction enable the true and keep in mind that this
value is by default true when you're working in an AI pad and false when you're working on a phone which
is described in the application.
So that's why I had to essentially manually fix that.
Now there are a few things we still have to do.
For a view controller class we have to conform to obviously you a view controller.
You also have to get the UI collection view delegate collection view delegate.
You also need to get the UI collection view drag delicate UI collection view drag delegate and the UI
collection view drop delegate.
So a lot of methods that we have to implement from a variety of these different are essentially delegations.
Now I'm actually going to write them down so we get a sense of what we have to be doing because there's
a few of these and we have to know what we have to do with them.
So these are the essentially the five pieces that we need someone to say they favor these the two of
them are the ones that we have been doing.
Number of items and then sell for them after all.
These two are essentially parts of the UI collection view data source which I have forgotten for you
eye collection view data collection view data source.
So these two are from the UI collection view a data source that we have done many many times before.
We then have to implement three other guys and those are for drag and drop those large items for beginning
drag.
So which item we are dragging right at the very beginning and then updating the drop session at the
drop.
So the moment you drop what you're going to do and then performing drop so should it drop.
Is it on top of the right index part and so on and so for there's so many things that we have to do
in here.
So let's get this started the easier one I'm going to say I have a value over I'm going to call.
I'd them count just like the last time.
So we know how many items you're going to have.
And I'm going to say number of items in section and we get rid of that so I can format things better.
There's going to be a return whatever we have in the item count.
So one is down easier one obviously the next one is sell for IDM.
I think next math something a game we have done multiple times so let's sell to be collection view dot
D Q reusing sell a D.
And for index card that is index card.
And I'm going to say return cell between these two I'm just gonna change the color so I'm going to say.
Bygones.
Color is you eye color dot red and if index part the raw modulus 2 is 0 then say sell background color
is your eye color dot blue.
So so far we should have a collection view of it at least 10 items that they are alternating between
colors of red and blue.
So another method is down as well.
Now we have to talk about these three methods.
So I'm going to add all three of them.
One of them is items for beginning items for beginning to adopt one.
We have a thing here and began to talk about what we have to have for it.
I'm going to say for now return nil because it essentially needs a an array of new either items for
members it just returned nothing because we don't know how this is going to work.
The next thing is updating the drop session.
So I'm going to say drop their obsession did the update.
If I'm not drawing did update.
So the obsession did update.
So this the next method that we have to implement at some point.
So this one we can't return and nil the value for it.
We actually have to return a UI collection view drop proposal and let me explain to you what that is.
Let's say my proposal is essentially what you are expecting to happen once the drop is done.
So when I say is going to be a UI collection view drop proposal analysts build one of these and then
what I want to build is this I'm going to go ahead and say I have two things and I have a drop operation
and I also have an intent for what I'm expecting to happen.
The operation that they want to do is let's go ahead and see the operations is actually moving because
I wanted to write and draw up.
I don't want to copy one.
I want to move one from one place to the other one so the operation type is move.
Now let's go ahead and talk about intent.
What do we expect to see.
Vendor intent happens.
So the intent on letting go ahead and say there are few things in here.
The most notable one is insert at destination X point or insert into a destination the X1 and I'm going
to go ahead and say I want to do an inset at destination and x part and remember the destination the
Smart is actually already built into this function it knows where you are trying to drop.
So when did I get that obsession begins which is in here.
We pick up some items for drag and drop and as we are going along and we drop them somewhere at the
drop point we have to decide what do we want to happen in there.
So this is a proposal.
This is a very he's going to drop it doesn't mean he's going to drop that it just means that it feels
like this is where it should be.
Do you want to do it or not.
And that's something we still have to decide about it.
We do either have to return this.
I'm going to say return my proposal.
Because he's expecting a huge collection of you drop proposal somewhere.
Return that value and say here's what I'm trying to do.
So one more is done.
So these two are taken care of.
And finally the important one is this one doing the actual drop performance.
What do you want to happen when we actually do did it.
So I'm going to go ahead and say perform drop so that is that and this fund doesn't have to have any
written values.
It just has to be there so they can actually test the application.
Now I know that everything is working so far because I kind of like to be able to draw on my apps once
130 is like ten minutes or so.
So let's run the application.
So now that the simulator is up I can see my items but a chance to need drag or drag and drop just yet.
Even I try to hold the button in here.
It doesn't really do anything simply because the value that we have in here either returns nil the correction
you drag and drop doesn't really know.
Shy them to begin that oxygen.
So that's the next thing we have to do.
So we now have to make an array of you drag items and since you only have one of them let's go ahead
and make just one.
So I'm going to say let's drag IDM to be a new drag item and the usually drag items have to be built
based on IDM providers and then provide there's essentially a way of conveying what kind of data has
to be going with the object you're dragging or dropping.
So before I could use a drink item I'm going to go ahead and say let my IDM provider.
And that's going to become an endless IDM provider and then as IDM provider you can make it with any
kind of object that conforms to NSA IDM provide their writing.
These are NSA strings and as they say they are things such as UI images.
So on and so forth.
Now for now the data that they want to send it doesn't exist simply because I don't have an array of
objects.
I only have an item count and I'm somewhere in the index spot.
So for instance here if you're the index tool that is there the are.
So that's the item I want to move.
So I'm going to go ahead and say let item to be index part doctoral.
Now if this was an application that you actually had an array of objects then you had to get your item
from that array.
So keep that in mind.
Now since we can make NSA then providers from things such as NSA stings I'm going to convert my IDM
into one NSA string I'm going to say I have the IDM as NSA string and then that is going to become my
then provide that are my item provider becomes the IDM provider for my drag IDM.
And finally I'm going to say my dad I gave them DOD no con object which could be any kind of object
becomes the item itself.
And of course we are returning that drag item routine are our rate of return.
Now if you're on the application by now we should be able to see the drag report happening.
We should be able to drag an object even though we still can't drop it anywhere.
So if I type in yet I can actually drag things and I can come in here and I try to up but nothing happens
simply because they're performed drop is not happening even though we know the a proposal we know where
we are trying to drop at the update of the drop but we don't know what should happen in the performance
drop.
So that's the next thing that I'm going to be working on.
So theoretically this object is the one that knows everything about your drop operation.
So I'm going to go ahead in here and say let index path and I'm going to make my index path to become
coordinator DOD destination index.
So that's the next part that we're trying to go to the set destination 1.
Now I would also like to make a suggestion.
I'm gonna say if left in the expand is that thing because simply you might have a situation where your
user is dropping your cell at the place that doesn't exist.
It is.
There is no destination in path in that situation.
If you use a drag something and drops it somewhere outside the collection view there is no index spot
there.
So you want to make sure that there is an index map.
And if that thing was happening let's go ahead and say let the items is the coordinator or the need
for those items.
So we get all the items that haven't been dropped.
Once I got the items I'm going to go ahead and see if I Dems that count is the one even though I know
it is one because we only drag one.
But I'm going to make a check for it in the NSA if there's only one thing go ahead and say let's I IDM
it to be a Dems dot first.
So let's get that very first one and then find out the various its source index spot some less a source
index but I'm going to go ahead and say that is IDM dot source index.
So we find the index part which is our destination index fund where we are trying to drop we find the
we are trying to drop up and from those we find only one of them and that one we forget our date is
coming from.
So now we have all the information we need.
So I'm going to go ahead and say since we have all of these do this say collection view dog perform
batch updates and as you're trying to perform the batch updates.
If you don't have to do anything at its completion so you can essentially drop the completion parts
say nil.
But for the update itself let's go ahead and do all of these things now.
The things that you want to be doing are these if you have an array if you have an array you have to
update that.
That's just like what you would do if you were deleting a set or if you were rearranging objects in
a table view.
So if you're moving an item between the cells of a collection of you you obviously have to update that
array to reflect these new changes.
You have to see my array but remove the objects of source index Mark and then insert objects at the.
In this case the destination expert which is the index fund.
Now once that is done I'm going to go ahead and say collection view do a delete item at source in the
spot so we won't delete that one which is an error rate because that expects an a rate delete a Dems.
And since it is an optional value I'm going to have to force unwrap it now that they got the delete
part taken care of.
I have to insert a new one.
I'm going to say collection view dot insert items and I'm going to insert at index back because that's
my destination index.
I'm going to say insert items at index but once this entire process is done lost our founding index
by the item the source index bad and we have performed all the updates both on our collection view as
well as our arrays we can go ahead and safely drop the objects so they could go ahead and say co-ordinator
dot to drop and I want to drop them the UI that I got them to the index path so I'm going to go ahead
and say the object you have been dragging which happens to be a Dems dot first dot dot I IDM and that
first has to be none optional obviously and then it says well you want to drop it I won't drop it at
the index spot.
So by now it finds out what are you trying to do updates every one and let's go of the object as being
drag which is this what it drops this from the application and if it all works well we should be able
to see that we can pick an object drag it drop it somewhere.
So now when I run the application I can actually go ahead and drag and drop objects and they perform
exactly the way we were hoping they would do.
Now keep in mind that this was a very basic drag and drop there's a lot more yet you have to do for
a real life application specifically because you will have real objects instead of a adjust they're
valuable that counts numbers you would have objects and you would have classes and then those if you
have an implication on how you do your item provided in here and what item you sent as your drag object
but everything else should work similarly in terms of finding your proposal the correct destination
in the expired as well as doing the update this after you have right now should give you the the bones
for developing pretty much any kind of dry gander up after this you have an assignment which is about
dragging and dropping between different collection views so let's move on into that one.