views:

1256

answers:

3

Do you have a sample code for the UIButton animation of the AppStore Price -> to Buy button. I tried a lot, but it doesn't work well with CAAnimationGroup (scale+translation) and it doesn't work with just setting it to the new frame-size in a UIView beginAnimations. The animation first sets the new width(immediatelly) and then the new origin .. so it is not the right effect ...

Thank you!

+2  A: 

Are you making your changes inside the Core Animation block?

I created a simple view-based iPhone application to test this. The view has two rounded rect UIButton objects:

  1. The first button is in the upper right corner with width 62 (and height 35) and initial title "$0.99". It is connected to the "button" outlet and "animate" action in the view controller. This is the button that will be animated when it is tapped.

  2. The second button is at the bottom of the screen with title "Reset" and is connected to the "reset" action in my view controller.

Here is the view controller code:

UIButtonAnimationTestViewController.h:

#import <UIKit/UIKit.h>

@interface UIButtonAnimationTestViewController : UIViewController {
    IBOutlet UIButton *button;
    CGRect originalFrame;
}

- (IBAction)animate;
- (IBAction)reset;

@end

UIButtonAnimationTestViewController.m:

#import "UIButtonAnimationTestViewController.h"

@implementation UIButtonAnimationTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    originalFrame = button.frame;
}

- (IBAction)animate {
    CGRect frame = button.frame;
    frame.origin.x -= 30;
    frame.size.width += 30;

    [UIView beginAnimations:@"button" context:nil];
    button.frame = frame;
    [button setTitle:@"" forState:UIControlStateNormal];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [button setTitle:@"BUY NOW" forState:UIControlStateNormal];
}

- (IBAction)reset {
    button.frame = originalFrame;
    [button setTitle:@"$0.99" forState:UIControlStateNormal];
}
gerry3
it does not create the right effect for me. The width is set first, so the Button moves to the new position afterwards. In AppStore the Right-X-Position of the right corners always stay at their position, *like* a scale and translation at the same time.
erazorx
My code works like the AppStore: the right side of the button does not move. Since we are making both of the changes (translate left and increase width) to a copy of the frame and then changing the button's frame inside the animation block, both changes must animate at the same time.
gerry3
That works flawlessly my friend.
iWasRobbed
+1  A: 

There is an issue with this when you use a threepart image background for the UIButton: http://openradar.appspot.com/7290242

This will first pop the button to the right, then move it to the left. This approach will not work

qwertybo
A: 

There is a nice tutorial that shows you how to make one of these without image resources.

http://www.icodejunkie.com/?p=74

Brandon Emrich