views:

104

answers:

2

I create my own number pad view successfully and I want to simulate the default number pad appear animated effect, but it can't work fine, it can't animate as default number pad, how do I modify?

I have a UITextField call "fNumber" and a custom number pad view call myNumberPadView and created by interface builder, when the "fNumber" is focued I will show the myNumberPadView animated, so I code as follow:


 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == fNumber){
        CGRect frame = CGRectMake(0, 270, 320, 230);
        myNumberPadView.frame = frame;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.75];
        frame.origin.x = 0;
        frame.origin.y = 270; 
        [UIView commitAnimations];
        [self.view addSubview:myNumberPadView];
    }
    return NO;
}

The "myNumberPadView" is show as a subview, it doesn't the "animate" effect as the default number pad, how can to make it to "animate" appear?

A: 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == fNumber){
    CGRect frame = CGRectMake(0, 270, 320, 230);
    myNumberPadView.frame = frame;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.75];
    frame.origin.x = 0;
    frame.origin.y = 270; 
   // [UIView commitAnimations];
    [self.view addSubview:myNumberPadView];//Place this before the line  [UIView commitAnimations];
    [UIView commitAnimations];
}
return NO;
}
Rahul Vyas
+1  A: 

in the animation block, just set the new frame to your custom numpad. smth like

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == fNumber){
        CGRect frame = CGRectMake(0, 480, 320, 230);
        [myNumberPadView setFrame:frame];
        [self.view addSubview:myNumberPadView];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.75];
        [myNumberPadView setFrame:CGRectMake(0, 270, 320, 230)]; 
        [UIView commitAnimations];
    }
    return NO;
}
Morion
Morion has it right. Add the subview below the screen where you can't see it, then start an animation block, set the frame on the view to where it should end up, and then commitAnimations. That will make it move smoothly from one location to the other.
Nimrod