UITextField清除按钮用于屏幕动画

时间:2014-09-02 22:38:52

标签: ios animation uitextfield uitextfielddelegate

当用户输入文本字段时,我有一个执行动画的屏幕(向上移动屏幕),以便在输入数据时保持可见,然后在用户输入数据后向下移动。我遇到的问题是当用户尝试使用文本框内的清除按钮时(使用site_TextField.clearButtonMode = UITextFieldViewModeAlways设置)。按下清除按钮时文本字段的流程应为:

textFieldShouldEndEditing - > textFieldDidEndEditing - > textFieldShouldClear - > textFieldShouldBeginEditing - > textFieldDidBeginEditing

但是,textFieldDidEndEditing方法是调用动画的位置,执行动画时,上面的序列停止,并且不调用textFieldShouldClear。我希望这是一件简单的事,我只是想念。任何意见都表示赞赏!

- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
    NSLog(@"In textFieldShouldBeginEditing");
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == site_TextField || textField == phone_TextField) {
        [self animateTextField: textField up: YES];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"In textFieldShouldEndEditing");
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == site_TextField || textField == phone_TextField) {
        [self animateTextField: textField up: NO];
    }
}

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

    NSLog(@"In textFieldShouldClear");
    return YES;
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    if (textField != site_TextField && textField != phone_TextField) {
        return;
    }

    int movementDistance = 0;
    if (textField == site_TextField) {
        movementDistance = 100;
    } else if (textField == phone_TextField) {
        movementDistance = 170;
    }

    int movement = (up ? -movementDistance : movementDistance);

    [UIView animateWithDuration:0.3
                          delay:0.0
                        options: UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
                    }
                     completion:^(BOOL finished){
                     }];
}

1 个答案:

答案 0 :(得分:0)

我怀疑通过触摸清除按钮触发了第一个textFieldShouldEndEditingtextFieldDidEndEditing。并且textFieldShouldClear由修饰触发。但是对于动画,textField会在触摸后立即移动,因此触摸不会注册。尝试使用textField didEndOnExit事件来触发一个向下移动动画的方法。