Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel transition #7

Closed
holidaycottages opened this issue Apr 2, 2014 · 8 comments
Closed

Cancel transition #7

holidaycottages opened this issue Apr 2, 2014 · 8 comments

Comments

@holidaycottages
Copy link

Hello again,

I have a situation where if i rotate my device while performing a layout transition the app errors with the following error:

the collection is already in the middle of an interactive transition

So I was wondering if there was a way I could cancel the transition if one was in progress?

@wtmoose
Copy link
Member

wtmoose commented Apr 2, 2014

What exactly do you want to happen on cancel?

Revert back to the starting point? Animated or not animated?
Stop in place?
Etc.?

Also, could you elaborate on why you're getting the error? For example, are you starting up a new interactive transition or is the collection view doing this on its own? None of my apps rotate, so I'm not very familiar with this.

@holidaycottages
Copy link
Author

Well when I tap on a cell to transition it to another layout but at the same time rotate the device, in which that invalidates the layout and changes the collectionview frame and then I make the call to re-layout the view, it causes the above error because its already in the middle of a transition.

So what do I need it to do? I guess if a transition is in progress then stop it and continue with the next, but I am not 100% thats the right thing...

@wtmoose
Copy link
Member

wtmoose commented Apr 4, 2014

There are only two ways to end an interactive layout transition:

-[UICollectionView cancelInteractiveTransition]
-[UICollectionView finishInteractiveTransition]

When you call one of these APIs, the collection view will takes over drives the progress to either 0 or 1 over a short period of time and then either revert to the original layout or install the next layout.

It sounds like what you really need is the ability to stop the transition immediately. I'm not aware of any API to do this, but I needed this too and spent a couple of days this week on a workaround. If you download the cancelinplace branch, there is a new API you can try:

-[UICollectionView cancelInteractiveTransitionInPlaceWithCompletion:]

Its working flawlessly in my app. You would do something like this:

if ([self.collectionView isInteractiveTransitionInProgress]) {
    [self.collectionView cancelInteractiveTransitionInPlaceWithCompletion:^(){
        // set new layout, start a new interactive transition, or whatever
    }];
}

The cancelInteractiveTransitionInPlaceWithCompletion call works something like this:

  1. Take a snapshot of the current state of the transition layout
  2. Set transition progress to 0
  3. Call [self cancelInteractiveTransition]
  4. When the transition completion block is called, install the snapshot layout
  5. Call the cancel-in-place completion block

In the UI, it should look like the collection view stopped at the point where you cancelled and it will stay that way until you replace the snapshot layout. You can start another interactive transition at this point and it will proceed from the snapshot layout to your next layout.

The only caveat I know of is that it will not work with layouts that have supplementary or decorative views because the snapshot layout doesn't support that yet.

@holidaycottages
Copy link
Author

Lovely Tim, I will give this a try for you and let you know. 👍

@holidaycottages
Copy link
Author

Morning, | have implemented this branch and I must say it does work very well, it no longer crashes when rotating quickly in the middle of a transition. Its an odd situation to have but I think some users will do it, if not by choice then accident! Thank you! I will do some further testing on a device today!

@wtmoose
Copy link
Member

wtmoose commented Apr 7, 2014

Good to hear it works. I’ve pushed the code to the master branch.

Tim

On Apr 7, 2014, at 2:24 AM, holidaycottages notifications@github.com wrote:

Morning, | have implemented this branch and I must say it does work very well, it no longer crashes when rotating quickly in the middle of a transition. Its an odd situation to have but I think some users will do it, if not by choice then accident! Thank you! I will do some further testing on a device today!


Reply to this email directly or view it on GitHub.

@clooth
Copy link

clooth commented Sep 11, 2014

So can we close this then? :)

@wtmoose
Copy link
Member

wtmoose commented Sep 11, 2014

Yep.

@wtmoose wtmoose closed this as completed Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants