使用动画移动文本字段

时间:2013-02-11 06:26:33

标签: iphone uitextfield

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

我使用的代码: -

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


}

3 个答案:

答案 0 :(得分:1)

对于IOS Swift

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()
    }

答案 1 :(得分: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];
    }
 }

答案 2 :(得分:0)