There are many situations where you need to "shake" a UIView.
(For example, "draw child user's attention to a control", "connection is slow", "user enters bad input," and so on.)
Would it be possible to do this using UIKit Dynamics?
So you'd have to ..
take the view, say at 0,0
add a spring concept
give it a nudge, say to the "left"
it should swing back and fore on the spring, ultimately settling again to 0,0
Is this possible? I couldn't find an example in the Apple demos. Cheers
Please note that as Niels astutely explains below, a spring is not necessarily the "physics feel" you want for some of these situations: in other situations it may be perfect. As far as I know, all physics in iOS's own apps (eg Messages etc) now uses UIKit Dynamics, so for me it's worth having a handle on "UIView bouncing on a spring".
Just to be clear, of course you can do something "similar", just with an animation. Example...
But that simply doesn't have the same "physics feel" as the rest of iOS, now.
-(void)userInputErrorShake
{
[CATransaction begin];
CAKeyframeAnimation * anim =
[CAKeyframeAnimation animationWithKeyPath:@"transform"];
anim.values = @[
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(-4.0f, 0.0f, 0.0f) ],
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(4.0f, 0.0f, 0.0f) ]
];
anim.autoreverses = YES;
anim.repeatCount = 1.0f;
anim.duration = 0.1f;
[CATransaction setCompletionBlock:^{}];
[self.layer addAnimation:anim forKey:nil];
[CATransaction commit];
}