iOS - 在UIView移动时将UITextField移动到不同的位置

时间:2011-08-27 21:45:46

标签: ios animation uiview uitextfield uiswitch

我有一个主要的UIView,它通过一个开关向上移动。我有这个工作,那里没有问题。现在UIView在关闭时占据了大约一半的屏幕,当它向上推时它显示底部的40px。

在UIView关闭时,它有一个UITextField,它位于中间的某个位置。当UIView被推高时,我希望UITextField移动到这些40px内,以便它始终可访问。

这是我为UIView移动的代码:

-(IBAction)mainview:(id)sender{

if (uiSwitch.on) {
    label.text = @"Push Up View";
    [UIView beginAnimations:@"animateView" context:nil];
    [UIView setAnimationDuration:0.5];
    CGRect viewFrame = [subView frame];
    viewFrame.origin.y  += 0;
    subView.frame = viewFrame;
    subView.alpha = 1.0; 
    [self.view addSubview:subView];
    [UIView commitAnimations];
}

if ([uiSwitch isOn] == YES) {
    [UIView beginAnimations:@"animateView" context:nil];
    [UIView setAnimationDuration:0.5];      
    CGRect viewFrame = [subView frame];
    viewFrame.origin.y  = 0;
    subView.frame = viewFrame;    
    [UIView commitAnimations];
}
else {
    label.text = @"Show View";   
    [UIView beginAnimations:@"returnView" context:nil];
    [UIView setAnimationDuration:0.5];  
    CGRect viewFrame = [subView frame];
    viewFrame.origin.y  = -230;
    subView.frame = viewFrame; 
    subView.alpha = 1.0;
    [UIView commitAnimations];
}}

UITextField尚未添加到此,这就是我被困住的地方。我知道我可能不得不为UITextField创建一个类似的IBAction,但我不确定如何为该位设置动画。

任何帮助???干杯杰夫

1 个答案:

答案 0 :(得分:10)

您可以同时为多个视图设置动画。你的代码:

label.text = @"Show View";   
[UIView beginAnimations:@"returnView" context:nil];
[UIView setAnimationDuration:0.5];  
CGRect viewFrame = [subView frame];
viewFrame.origin.y  = -230;
subView.frame = viewFrame; 
subView.alpha = 1.0;
[UIView commitAnimations];

..可以在你的UITextField上包含额外的帧操作:

label.text = @"Show View";   
[UIView beginAnimations:@"returnView" context:nil];
[UIView setAnimationDuration:0.5];  
CGRect viewFrame = [subView frame];
viewFrame.origin.y  = -230;
subView.frame = viewFrame; 
subView.alpha = 1.0;
// let's move our textField too
CGRect textFieldFrame=textField.frame;
frame.origin.y+=40;
textField.frame=textFieldFrame;
[UIView commitAnimations];

请注意,如果您的目标是iOS 4.x或更高版本,则可以依赖动画块:

[UIView animateWithDuration:0.5 animations:^{

    CGRect frame;

    // move our subView to its new position
    frame=subView.frame;
    frame.origin.y=-230;
    subView.frame=frame;
    subView.alpha=1.0;

    // let's move our textField too
    frame=textField.frame;
    frame.origin.y=40;
    textField.frame=frame;

}];