2012-04-04 11 views
2

So, ich habe eine UIButton, die, wenn eine UITextField im Bearbeitungsmodus ist, wird ausgeblendet und eingeblendet. Das Problem ist, dass es sich vollkommen ändert (von versteckt zu unsichtbar), aber nicht animiert. Ich setAlpha: stattdessen habe versucht, aber das funktioniert nur, wenn er seine Alpha von 0 bis 100 einstellen, nicht 100 auf 0. Hier ist mein Code so weit:Animate UIButton wird versteckt und eingeblendet

-(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: Ich löste das Problem. Ich musste einfach nicht die removeFromSuperview Funktion aufrufen, und ich musste von versteckt zu Alpha wechseln. (Siehe @ Davids Antwort unten)

Antwort

1

Sie haben ein Problem mit Ihren Animationen. Ändern Sie Folgendes:

-(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]; 
} 

Sie haben die Schaltfläche sofort aus der Ansicht entfernt, anstatt sie nach dem Ausblenden zu entfernen.

Prost!

+0

Hallo David, danke für die Hilfe, aber ich habe diese Methode ausprobiert, und die Schaltfläche erscheint nicht einmal. –

+0

Haben Sie alle Hiddens in Alphas geändert? – David

+0

Ja. Meine Bearbeitung wurde oben gepostet, ich habe die Lösung gefunden. –

Verwandte Themen