Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS icon jiggle algorithm

Tags:

I am writing an iPad app that presents user documents similar to the way Pages presents them (as large icons of the actual document). I also want to mimic the jiggling behavior when the user taps the edit button. This is the same jiggle pattern that icons make on the home screen when you tap and hold on them on both the iPhone and iPad.

I've searched the Internet and have found a few algorithms but they just cause the view to rock back and forth which is not at all like the Apple jiggle. It seems there is some randomness in there as each icon jiggles a little differently.

Does anyone have or know of some code that can re-create the same jiggle pattern (or something very close to it)? Thanks!!!

like image 419
Vic320 Avatar asked Jul 06 '11 23:07

Vic320


People also ask

How do I make my iPhone icons jiggle?

Press the Digital Crown (the knob on the side), then tap and hold any app icon until the apps begin to jiggle. After that you can hold and drag any app icon to a new location. When you're done, press the Digital Crown again.

How do you trigger jiggle mode?

Also called "jiggle mode," it is activated by tapping and holding any icon for a couple of seconds until all the icons start fluttering.

Why do iPad icons jiggle?

The icons will wiggle if you tap and hold on any of them. Once they start doing this, you can move them from one screen to another or, if they are not preinstalled apps, delete them. All you have to do to make them stop is tap the Home button.

Why do apps shake on iPhone?

Your apps are shaking because you held down on one too long, allowing all your apps to be moved or deleted. In order to move an app while the apps are shaking, hold down on one, and move it about. In order to delete an app while the apps are shaking, tap the black and white X in the corner of the app.


2 Answers


@Vic320's answer is good but personally I don't like the translation. I've edited his code to provide a solution that I personally feel looks more like the springboard wobble effect. Mostly, it's achieved by adding a little randomness and focusing on rotation, without translation:

#define degreesToRadians(x) (M_PI * (x) / 180.0) #define kAnimationRotateDeg 1.0  - (void)startJiggling {     NSInteger randomInt = arc4random_uniform(500);     float r = (randomInt/500.0)+0.5;      CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( (kAnimationRotateDeg * -1.0) - r ));     CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg + r ));       self.transform = leftWobble;  // starting point       [[self layer] setAnchorPoint:CGPointMake(0.5, 0.5)];       [UIView animateWithDuration:0.1                            delay:0                          options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse                        animations:^{                                   [UIView setAnimationRepeatCount:NSNotFound];                                  self.transform = rightWobble; }                       completion:nil]; } - (void)stopJiggling {     [self.layer removeAllAnimations];     self.transform = CGAffineTransformIdentity; } 


Credit where credit's due though, @Vic320's answer provided the basis for this code so +1 for that.

like image 158
imnk Avatar answered Sep 18 '22 16:09

imnk


OK, so the openspringboard code didn't quite do it for me but I did allow me to create some code that I think is a bit better, still not prefect but better. If anyone has suggestions to make this better, I would love to hear them... (add this to the subclass of the view(s) you want to jiggle)

#define degreesToRadians(x) (M_PI * (x) / 180.0) #define kAnimationRotateDeg 1.0 #define kAnimationTranslateX 2.0 #define kAnimationTranslateY 2.0  - (void)startJiggling:(NSInteger)count {      CGAffineTransform leftWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? +1 : -1 ) ));     CGAffineTransform rightWobble = CGAffineTransformMakeRotation(degreesToRadians( kAnimationRotateDeg * (count%2 ? -1 : +1 ) ));     CGAffineTransform moveTransform = CGAffineTransformTranslate(rightWobble, -kAnimationTranslateX, -kAnimationTranslateY);     CGAffineTransform conCatTransform = CGAffineTransformConcat(rightWobble, moveTransform);      self.transform = leftWobble;  // starting point      [UIView animateWithDuration:0.1                           delay:(count * 0.08)                         options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse                      animations:^{ self.transform = conCatTransform; }                      completion:nil]; }  - (void)stopJiggling {     [self.layer removeAllAnimations];     self.transform = CGAffineTransformIdentity;  // Set it straight  } 
like image 25
Vic320 Avatar answered Sep 21 '22 16:09

Vic320