2012-04-04 16 views
2

UITextFieldが編集モードになっているときに、非表示と非表示になるUIButtonがあります。問題は、(隠されたものから隠されていないものまで)完全に細かく変化するが、アニメーション化されないことである。私が代わりにsetAlpha:試してみたが、ここで、これまでに私のコードではありません100 0に、0から100までのアルファを設定しているときにのみ動作します。アニメーションのUIButtonが非表示になり、非表示になる

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

編集:私はこの問題を解決しました。私はちょうどremoveFromSuperview関数を呼び出す必要はなかった、私はアルファからアルファに切り替える必要があった。 (下記の@ Davidの答えをご覧ください)

答えて

1

あなたのアニメーションに問題があります。次のように変更してください。

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

フェードアウト後にボタンを削除するのではなく、ビューからすぐに削除していました。

乾杯!

+0

こんにちはデビッド、助けてくれてありがとう、私はこの方法を試して、ボタンが表示されません。 –

+0

ヒッデンズをすべてアルファに変更しましたか? – David

+0

はい。私の編集は上に掲載されている、私は解決策を見つけた。 –

関連する問題