Rubber button with UIKit dynamics

I would like to create a rubber button in my iOS application: when the user presses the button, he reduces his frame, and when the user releases the tap, he will grow, expand a little, and then return to the original with the bouncing effect, as if it were made from rubber. I saw this button in some other applications, and I'm sure that they used the iOS 7 UIKit dynamics to implement this effect. I tested the different behavior of the UIKit dynamics, but none of them seem useful, since none of them resize the frame. I was wondering if there is a hint on how to implement this behavior?

+3
source share
4 answers

This is probably not UIKitDynamics, but just an animation.

UIKitDynamics used to move objects around the screen and apply physics to collisions, etc.

What you can use is a new animation method ...

- (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay
     usingSpringWithDamping:(CGFloat)dampingRatio
      initialSpringVelocity:(CGFloat)velocity
                    options:(UIViewAnimationOptions)options
                 animations:(void (^)(void))animations
                 completion:(void (^)(BOOL finished))completion;

This will enliven the change, but apply the spring effect to the animation curve.

If you change the frame of the button inside the animation block, it will be the "spring" of the new frame and looks rubber.

You can learn more about this here ... http://www.oliverfoggin.com/animate-with-springs/

+11
source

According to Vogmeister, spring damped animation is your friend. If you want to target older versions of iOS, you can write the animation code in these lines:

[UIView animateWithDuration:duration animations:^{
    // increase to 1.1 of the final size
} completion:^(BOOL finished) {
    // reduce to final size in new animation block
}

Although this is clearly not as flexible as the spring mechanics in iOS 7, it looks very similar visually.

0
source

As @Fogmeister pointed out, the easiest way is to animate the button’s scale using the new UIView“animate ... usingSpring ...” method of iOS 7. I downloaded the gists sample for your link at https://gist.github.com/mrtj/9089823

0
source

The UIKit Dynamics Catalog sample code contains a demonstration of what you are trying to achieve. See here for more details .

In principle, you will need a combination of UIAttachmentBehaviorand UIPushBehavior.

0
source

All Articles