iOS - 当UIView移动时将UITextField移动到不同的位置 [英] iOS - Moving a UITextField to a Different Position when a UIView Moves

查看:85
本文介绍了iOS - 当UIView移动时将UITextField移动到不同的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main UIView which moves up by means of a switch. I have this working and there is no problem there. Now the UIView when down, takes up about half of the Screen and when it is pushed up it shows the bottom 40px.

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

In the UIView when it is down it has a UITextField and it sits somewhere in the middle. When the UIView is pushed up I want that UITextField to move to sit within those 40px, so that it always accessible.

这是我为UIView移动的代码:

This is the code I got for the UIView to move:

-(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,但我不知道如何设置该位的动画。

The UITextField is not added to this yet and that is where I am stuck. I know I will probably have to create a similar IBAction for the UITextField, but I am not sure how to animate that bit.

任何帮助???干杯杰夫

Any Help??? Cheers Jeff

推荐答案

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

You can animate multiple views at the same time. Your code:

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上包含额外的帧操作:

.. can include additional frame manipulations on your 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或更高版本,则可以依赖动画块:

Note that if you're targeting iOS 4.x or later, you can rely on animation blocks:

[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;

}];

这篇关于iOS - 当UIView移动时将UITextField移动到不同的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆