4

I started trying to recreate the buy button from the app store which requires a 2-stage click to buy something. I to animate the button expanding. So far I have this

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];

sender.autoresizesSubviews = NO;
sender.clipsToBounds = NO;
sender.frame = CGRectMake(63,326,200,37);

[UIView commitAnimations];

which just causes the button to become larger, it doesn't animate at all. Have I done something wrong or has anyone else implemented this type of button behaviour?

EDIT:

- (IBAction) buyButtonAction: (UIButton *) sender {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationDelay:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

sender.clipsToBounds = NO;

sender.frame = CGRectMake( CGRectGetMinX( sender.frame) - 30, CGRectGetMinY(sender.frame), 200, 37);
[sender setTitle:@"Touched Touched Touched" forState:UIControlStateNormal];


[UIView commitAnimations];
}
Josh
  • 3,445
  • 5
  • 37
  • 55

1 Answers1

7

Are you targeting an iOS that doesn't support Blocks?

I've implemented a "button animates on touch" using the following nauseatingly simple code.

[UIView animateWithDuration:0.5 animations:^{
    self.navigationItem.rightBarButtonItem.title = @"Quoting...";
}];

Alternatively, this code seems to work as well to animate a button on touch, if you can't support blocks (it also includes the blocks commented out if you go that route):

-(IBAction) clicked:(UIButton*)sender{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    //[UIView animateWithDuration:2.5 animations:^{

    sender.autoresizesSubviews = NO;
    sender.clipsToBounds = NO;
    sender.frame = CGRectMake(63,326,200,37);

    //sender.frame = CGRectMake( CGRectGetMinX( self.theButton.frame) - 100, CGRectGetMinY(self.theButton.frame), 300, 40);
    //[sender setTitle:@"Touched Touched Touched" forState:UIControlStateNormal];
//}];
LrdCasimir
  • 123
  • 5
  • I think I must be doing something wrong. I've used the longer snippet you provided and now the size immediately changes and then the only thing that's animated is moving the location of the button?!?! I've made an edit to the original post to show you where the code lies. – Josh Aug 04 '11 at 11:25
  • I dropped your property changes into my sample code, taking out the setTitle, since I don't think that part animates properly. I put in a location - 30 for the button's X coordinates for my code. I'll edit my code to use your animations and perhaps that will provide useful results. – LrdCasimir Aug 04 '11 at 12:22
  • Awesome, thanks. No idea what was wrong originally, but got it sorted now thanks. – Josh Aug 04 '11 at 12:34