TeehanLax / UICollectionView-Spring-Demo

A demonstration of UICollectionView and UIKit Dynamics
MIT License
471 stars 71 forks source link

'Twitching' when system stops #1

Closed marcregan closed 11 years ago

marcregan commented 11 years ago

If you stop scrolling the system, you'll see that it takes about 4 seconds for the system to come completely to a rest. You'll notice the blue boxes bouncing about one pixel up and down.

Somehow Apple has found a way around this as the Messages app doesn't have this problem. If you look carefully at the way the Messages cells work, there are subtle differences that I think could provide clues to their implementation:

I suspect they are applying an animation to all the cells to bring them to a stop when the user releases their finger, but I haven't been able to reproduce the effect. They've added a spring based animation to UIKit in iOS7 that might be the key to this. If I have some time this week I'll see if I can help.

Thanks for building this!

ashfurrow commented 11 years ago

Hmm. I tried just modifying the properties of the attachment behaviour – no joy. Any one else have any ideas? Maybe an action on the attachment behaviours that sets their length to zero once they're close enough to zero?

ashfurrow commented 11 years ago

This is really intriguing. May have to burn one of my technical support tickets on this.

ole commented 11 years ago

A while ago I read on Twitter that the cells in Messages.app are actually not implemented with UIKit Dynamics. Don't know if true, though.

marcregan commented 11 years ago

There is a thread in the Apple developer forum about this problem (requires an Apple Developer Account):

https://devforums.apple.com/message/891070#891070

The last guy who responded claimed it could be fixed by adding multiple springs between each element and tuning the numbers, but I didn't have any luck there.

Marc Regan http://about.me/marcregan

On Tuesday, October 1, 2013 at 11:24 AM, Ash Furrow wrote:

This is really intriguing. May have to burn one of my technical support tickets on this.

— Reply to this email directly or view it on GitHub (https://github.com/TeehanLax/UICollectionView-Spring-Demo/issues/1#issuecomment-25458869).

ashfurrow commented 11 years ago

I think I've got it – set the length of the attachment behaviour to 1 so that it doesn't continue to "pull" as long after the finger has been released. @marcregan can you try this and confirm if it works for you too?

ashfurrow commented 11 years ago

Of course, this leaves the cells unevenly spaced, but for some applications it could be a viable solution. screen shot 2013-10-01 at 2 08 15 pm

marcregan commented 11 years ago

@AshFurrow Oh yeah! Nice! I switched the length = 1 for the attachment behavior and no more twitching.

Bummer about the cell spacing. In my particular app, it isn't that noticeable, so maybe it's OK.

I can't say I totally understand why this works - I'll have to watch the video again :)

kovpas commented 11 years ago

Twitching is really visible if you set pagingEnabled to YES. Cell spacing could also be crucial for someone (like me :)).

It seems that proper layout without twitching could be achieved with an additional spring, as it's said on apple dev forums (thanks @marcregan for the link):

    UIAttachmentBehavior *springBehaviour = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:center];
    springBehaviour.length = 0.0f; // <- set length back to 0, to make cell position properly
    springBehaviour.damping = 0.8f;
    springBehaviour.frequency = 1.0f;
    [self.dynamicAnimator addBehavior:springBehaviour];

    springBehaviour = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:CGPointMake(center.x, center.y - 1)];
    springBehaviour.length = 1.0f;
    springBehaviour.damping = 0.8f;
    springBehaviour.frequency = 1.0f;
    [self.dynamicAnimator addBehavior:springBehaviour];

The first spring is the original one (I set length back to 0), which puts cell back on it's position, the second one is a spring which doesn't let cell to go over the center.

Works pretty well for me.