user1163722
user1163722

Reputation:

Animate UIButton becoming hidden and unhidden

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)

Upvotes: 2

Views: 2036

Answers (1)

Liftoff
Liftoff

Reputation: 25372

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!

Upvotes: 1

Related Questions