Animate UIButton becoming hidden and unhidden

Go To StackoverFlow.com

2

So, I have a UIButton that, when a UITextField is in editing mode, becomes hidden and unhidden. The problem is, it changes perfectly fine (from hidden to unhidden), but doesn't animate. I've tried setAlpha: instead, but that only works when it is setting its alpha from 0 to 100, not 100 to 0. Here is my code so far:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
negButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
negButton.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 37, textField.frame.size.height);
[negButton setHidden:YES];

return YES;
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEditing])
{
    [UIView animateWithDuration:0.3 animations:^
     {
         CGRect frame = textField.frame;

         frame.size.width -= 40;
         frame.origin.x += 40;

         [negButton setHidden:NO];
         [textField setFrame:frame];
         [self.view addSubview:negButton];
     }];
}
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.3 animations:^
     {

         CGRect frame = textField.frame;
         frame.size.width += 40;
         frame.origin.x -= 40;

         [negButton setHidden:YES];
         [negButton removeFromSuperview];

         [textField setFrame:frame];
     } 
     ];
}

EDIT: I resolved the issue. I just didn't have to call the removeFromSuperview function, and I had to switch from hidden to alpha. (See @David's answer below)

2012-04-04 22:50
by NoName


1

You have a problem with your animations. Change it to the following:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
negButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
negButton.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 37, textField.frame.size.height);
[negButton setAlpha:0]; 
[self.view addSubView:negButton];

return YES;
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEditing])
    {
     CGRect frame = textField.frame;

     frame.size.width -= 40;
     frame.origin.x += 40;

     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:0.3];
     [negButton setAlpha:1];
     [textField setFrame:frame];
     [UIView commitAnimations];
    }
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{

     CGRect frame = textField.frame;
     frame.size.width += 40;
     frame.origin.x -= 40;

     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:0.3];
     [negButton setAlpha:0];
     [textField setFrame:frame];
     [UIView commitAnimations];

     [self performSelector:@selector(removeBtn) withObject:negButton afterDelay:0.3];
}

- (void)removeBtn:(UIButton*)button
{
    [button removeFromSuperView];
}

You were removing the button from the view immediately instead of removing it after it had faded out.

Cheers!

2012-04-04 22:59
by David
Hi David, thanks for the help, but I tried this method, and the button isn't even appearing - NoName 2012-04-04 23:09
Did you change all of the hiddens to alphas - David 2012-04-04 23:14
Yes. My edit is posted above, I found the solution - NoName 2012-04-04 23:17
cough That's what I posted at the end of my answer - David 2012-04-04 23:18
I know, but your code didn't work. I posted that exact same code in to my project, and nothing happened. Thanks for pointing out the issue, though - NoName 2012-04-04 23:20
Huh, it works just fine in XCode for me...oh well, whatever - David 2012-04-04 23:21
Here, I'll give you credit in the edit : - NoName 2012-04-04 23:21
OH! You don't have it in the code adding the button as a subview - David 2012-04-04 23:21
Check the edit - NoName 2012-04-04 23:22
So THAT's what it was - NoName 2012-04-04 23:22
Yeah. Edited my answer. +1 please - David 2012-04-04 23:23
I +1'd it and accepted the answer - NoName 2012-04-04 23:23
Ads