动画UITextField

时间:2015-01-04 17:47:01

标签: ios animation swift uibutton

我需要这样做,以便当我点击一个按钮时,UITextField会从view外部转移到view。但是,当我执行以下代码时,UITextField在viewcontroller的中间开始,然后当单击该按钮时,它会从view外部向左转换到view。我如何才能使它在最初加载视图时,直到单击按钮时才会看到它,使用swift。

@IBAction func joinCircleButton(sender: AnyObject) {
    let button = sender as UIButton
    joinTextField.frame.origin.x=500
    joinTextField.frame.origin.y=100
    if (button.frame.origin.x - 75>0){
        UIView.animateWithDuration(0.5, animations:{
            button.frame = CGRectMake(button.frame.origin.x - 125, button.frame.origin.y,button.frame.size.width, button.frame.size.height)
            button.transform = CGAffineTransformMakeScale(0.5, 0.5);
            self.joinTextField.frame=CGRectMake(self.joinTextField.frame.origin.x - 325, self.joinTextField.frame.origin.y,self.joinTextField.frame.size.width, self.joinTextField.frame.size.height)
        })
    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用名为

的函数将UITextField或您要隐藏的任何对象放在视图之外。
func viewDidLayoutSubviews()

在加载屏幕之前调用此函数。它可以准备好子视图,但还没有在屏幕上显示它们。所以这是一个隐藏UITextField的机会,如下所示:

 func viewDidLayoutSubviews(){

 // Here is just an EXAMPLE of what I'd do with my text field, you can change this however you wish
 // The point is that I am putting it away so no one can see it at first, and then later it will show
 joinTextField.center = CGPointMake(joinTextField.frame.origin.x-500, joinTextField.frame.origin.y)

}

现在应该在视图加载时隐藏文本字段,然后可以在以后对其进行动画处理。我希望这有帮助。