2011-11-10 10 views
5

UITextFieldをアニメーション化しようとしていますが、厄介で奇妙な問題が発生しました。アプリケーションは、UITextFieldを有するカメラUIButton、それが限界の外に配置されているので示されていないカメラボタン後UIButtonを打ち消す第1の状態にUITextFieldをアニメートするときの奇妙な動作

first state

:私のアニメーションは、以下の配列を有します。アプリケーションの実際に自分のアプリケーションは、幅320があり、キャンセルボタン原点はUITextField上のユーザタッチは、カメラボタンのアルファチャンネルが0に設定されている第2の状態で(350,7)

second state

、原点のありますキャンセルボタンは(247,7)に設定されています。

final state

ユーザーがキーボードの戻るボタンまたはキャンセルボタンに触れた後、最終的な状態は最初の状態と同じです。

処理中に、UITextFieldの幅とx軸のキャンセルボタンの原点がアニメーション化されます。

私のコードは次のとおりです。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    barcodeSearchButton.userInteractionEnabled = NO; 
    cancelButton.userInteractionEnabled = NO; 

    CGFloat cancelButtonXOffset = CGRectGetMaxX(barcodeSearchButton.frame) - cancelButton.frame.size.width; 
    CGFloat searchFieldWidth = searchField.frame.size.width - cancelButton.frame.size.width + barcodeSearchButton.frame.size.width; 
    [UIView animateWithDuration:0.3 
     animations:^{ 
      searchField.frame = CGRectMake(searchField.frame.origin.x, searchField.frame.origin.y, searchFieldWidth, searchField.frame.size.height); 
      cancelButton.alpha = 1.0;     
      cancelButton.frame = CGRectMake(cancelButtonXOffset, cancelButton.frame.origin.y, cancelButton.frame.size.width,cancelButton.frame.size.height); 

      barcodeSearchButton.alpha = 0.0; 

     } 
     completion:^(BOOL finished) { 
      barcodeSearchButton.userInteractionEnabled = NO; 
      cancelButton.userInteractionEnabled = YES; 
     }];  
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    barcodeSearchButton.userInteractionEnabled = NO; 
    cancelButton.userInteractionEnabled = NO; 

    [UIView animateWithDuration:0.3 
       animations:^{ 
        searchField.frame = CGRectMake(searchField.frame.origin.x, searchField.frame.origin.y, searchFieldStartWidth, searchField.frame.size.height); 
        cancelButton.alpha = 0.0; 
        cancelButton.frame = CGRectMake(cancelButtonStartX, cancelButton.frame.origin.y, cancelButton.frame.size.width,cancelButton.frame.size.height); 
        barcodeSearchButton.alpha = 1.0;     
       } 
       completion:^(BOOL finished) { 
        barcodeSearchButton.userInteractionEnabled = YES; 
        cancelButton.userInteractionEnabled = NO; 
       }];   
} 

アニメーションを意味し、初回のみ、最終的な状態になるまで実行された場合、ユーザーはフィールド上(この部分は重要である)、いくつかのテキストを書き留め、UITextFieldの上で触れましたその後、ユーザーがキーボード戻りボタンをタッチすると、問題が表示されます。問題は、UITextFieldの幅がアニメーション化されても、UITextFieldの型付きテキストがアニメーション化されることです。

アニメーションは怒鳴るように振る舞う:

  1. UITextFieldストレッチバックその初期値に設定します。
  2. カメラボタンが1.0
  3. に設定され、その最初の原点にボタンバックをキャンセル入力したテキストは、UITextFieldの左上隅から飛ぶと、それがなくなっていないはずTHEそれが位置にUITextfieldに土地。

問題は、ステップ4が起こらず、問題が1回だけ発生することです。プロセスを再度開始する場合(uitextフィールドに触れる、テキストを入力する、リターンキーを押す)、手順4は実行されません。

私はこの問題を解決しようとして一日を費やしましたが、失敗しました。

答えて

0

をいくつかのプレースホルダーテキスト検索フィールドに、 _searchField.text = @ "プレースホルダ" を設定し、また _searchFieldを設定します。clearsOnBeginEditing = YES;

それともtextDidBeginEditing

+0

また、あなたの色と混乱、あなたにもそれを修正する必要がありますことので代わりにプレースホルダーテキストプロパティのテキストを使用して。 –

0

{修正を改善するために、編集} 上のプレースホルダのテキスト に等しいだ場合は、タイムアウト値だけで明確な私はこれと同じ問題、およびいくつかの詳細だけでなく、小さなを持つことができます実際に私のためにそれを修正しているハック。

だから、最初の詳細:

  1. 問題はフレームアニメーションが UITextFieldを拡大し、入力したテキストがフィールドに存在する最初の時間を発生します。 UITextFieldが再度編集され、同じアニメーションが再び発生した場合は、 問題は発生しません。
  2. searchField.clearsOnBeginEditing = YES;は問題を解決しません。
  3. 問題が発生したたびにプレースホルダーテキストを使用しました が観察されました。結果に影響はありません。
  4. 1フレームのテキストプロパティを変更すると、問題が解決されます。ここで

私はそれを修正するためにやっているものです:

viewDidLoadでは、私は空の文字列に一度割り当てを派遣、その後、1つのスペースにテキストプロパティを設定しています。シュリンクとUITextFieldを拡大するだけでなく、画面上のキャンセルボタンを移動する私の方法で次に

-(void)viewDidLoad { 
    searchField.text = @" "; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     searchField.text = @""; 
    }); 
} 

、私はこれは、瞬間アニメーションあるかどうかを確認しては0.0fに期間を設定します。

- (void)showCancelButton { 
    NSTimeInterval duration = 0.3f; 

    [UIView animateWithDuration:duration animations:^{ 
     CGPoint buttonOrigin = cancelButton.frame.origin; 
     CGSize buttonSize = cancelButton.frame.size; 
     CGFloat buttonTravelDist = buttonSize.width + 10.0f; 
     CGPoint onscreenPos = CGPointMake(buttonOrigin.x - buttonTravelDist, buttonOrigin.y); 

     cancelButton.frame = CGRectMake(onscreenPos.x, onscreenPos.y, buttonSize.width, buttonSize.height); 

     CGPoint fieldOrigin = searchField.frame.origin; 
     CGSize fieldSize = searchField.frame.size; 

     searchField.frame = CGRectMake(fieldOrigin.x, fieldOrigin.y, fieldSize.width - buttonTravelDist, fieldSize.height); 
    }]; 
} 

- (void)hideCancelButton { 
    NSTimeInterval duration = 0.3f; 

    [UIView animateWithDuration:duration animations:^{ 
     CGPoint buttonOrigin = cancelButton.frame.origin; 
     CGSize buttonSize = cancelButton.frame.size; 
     CGFloat buttonTravelDist = buttonSize.width + 10.0f; 
     CGPoint onscreenPos = CGPointMake(buttonOrigin.x + buttonTravelDist, buttonOrigin.y); 

     cancelButton.frame = CGRectMake(onscreenPos.x, onscreenPos.y, buttonSize.width, buttonSize.height); 

     CGPoint fieldOrigin = searchField.frame.origin; 
     CGSize fieldSize = searchField.frame.size; 

     searchField.frame = CGRectMake(fieldOrigin.x, fieldOrigin.y, fieldSize.width + buttonTravelDist, fieldSize.height); 
    }]; 
} 

これは私の状況を完全に修正しました。

6

みんなの回答に感謝しますが、以前は解決策を見つけました。 UITextFieldでアニメーションを開始する正しい方法は、キーボードの状態に応じて、textFieldShouldBeginEditingtextFieldShouldEndEditingのメソッドをオーバーライドしています。

初めての試みでは、textFieldDidBeginEditingtextFieldDidEndEditingをオーバーライドしてアニメーションを作成しました。アニメーションの理由がtextFieldShouldBeginEditingtextFieldShouldEndEditingではなく、textFieldDidBeginEditingtextFieldDidEndEditingでないことがわかります。しかし、それだけで動作します。

マイ改訂コードバージョン:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 

    CGFloat cancelButtonXOffset = CGRectGetMaxX(barcodeSearchButton.frame) - cancelButton.frame.size.width; 
    CGFloat searchFieldWidth = searchField.frame.size.width - cancelButton.frame.size.width + barcodeSearchButton.frame.size.width; 
    [UIView animateWithDuration:0.3 
     animations:^{ 
      searchField.frame = CGRectMake(searchField.frame.origin.x, searchField.frame.origin.y, searchFieldWidth, searchField.frame.size.height); 
      cancelButton.alpha = 1.0;     
      cancelButton.frame = CGRectMake(cancelButtonXOffset, cancelButton.frame.origin.y, cancelButton.frame.size.width,cancelButton.frame.size.height); 

      barcodeSearchButton.alpha = 0.0; 

     } completion:^(BOOL finished) { 
      searchField.clearButtonMode = UITextFieldViewModeAlways; 
     }]; 
    return YES; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { 
    [UIView animateWithDuration:0.3 
     animations:^{ 
      searchField.frame = CGRectMake(searchField.frame.origin.x, searchField.frame.origin.y, searchFieldStartWidth, searchField.frame.size.height); 
      cancelButton.alpha = 0.0; 
      cancelButton.frame = CGRectMake(cancelButtonStartX, cancelButton.frame.origin.y, cancelButton.frame.size.width,cancelButton.frame.size.height); 
      barcodeSearchButton.alpha = 1.0;     
     } completion:^(BOOL finished) { 
      searchField.clearButtonMode = UITextFieldViewModeNever; 
     }]; 
    return YES; 
} 
+0

これはうまくいきます。 UIControlEventEditingDidEndを使用しているときにUITextFieldsをアニメーション化することに関連した別の問題を修正しました。この設定に切り替え、アニメーションが修正されました。 – jasonjwwilliams

+0

これはiOS 8.1では動作しません(少なくとも)。 –