0

我有 6 个文本字段,我不想将前 3 个文本字段向上移动。但我想要休息。当我点击 1 个文本字段时它不会移动,但是当我点击 2,3 等文本字段时,视图向下移动。

我使用的代码:-

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{

    const int movementDistance = 105; 

    const float movementDuration = 0.3f;

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

    [UIView beginAnimations: @"anim" context: nil];

    [UIView setAnimationBeginsFromCurrentState: YES];

    [UIView setAnimationDuration: movementDuration];

    self.view.frame = CGRectOffset(self.view.frame, 0, movement);

    [UIView commitAnimations];

}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{

if (textField==txtname) {

       toolphone.hidden = YES;
    }

    if (textField==txtcompanyname) {
       toolphone.hidden = YES;
    }
    if (textField==txtemail) {
        toolphone.hidden = YES;

    }

    if (textField ==txtsubject) {
        toolphone.hidden = YES;
    }

    if (textField ==txtphone) {
        [txtphone resignFirstResponder];
        toolphone.hidden = NO;
    }

    if (textField ==txtmessage) {
        toolphone.hidden = YES;
        [self animateTextField: textField up: YES];
    }


}
4

3 回答 3

1

对于 IOS 斯威夫特

func textFieldDidBeginEditing(textField: UITextField) {
        animateTextField(textField, up: true)
    }
    
func textFieldDidEndEditing(textField: UITextField) {
        animateTextField(textField, up: false)
    }
func animateTextField (textField:UITextField, up:Bool) {
        
        var movementDistance = 80
       
        var movementDuration = 1.0
        var movement = CGFloat(up ? -movementDistance : movementDistance)
        UIView.beginAnimations("anim", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration)
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
        UIView.commitAnimations()
    }
于 2015-01-06T11:04:54.173 回答
0

您可以检查文本字段名称以在特定文本字段上执行动画。

-(void) animateTextField: (UITextField*) textField up: (BOOL) up 
{
    if (textField!=1sttextfieldname||textField!=2ndtextfieldname||textField!=3rdtextfieldname) {
    const int movementDistance = 105;

    const float movementDuration = 0.3f;

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

    [UIView beginAnimations: @"anim" context: nil];

    [UIView setAnimationBeginsFromCurrentState: YES];

    [UIView setAnimationDuration: movementDuration];

    self.view.frame = CGRectOffset(self.view.frame, 0, movement);

    [UIView commitAnimations];
    }
 }
于 2013-02-11T06:33:48.110 回答