简体   繁体   中英

Animate UITextField

I need to make it so that when I click a button, the UITextField transforms left onto the view from outside the view . However, when I execute the following code, the UITextField starts off in the middle of the viewcontroller, and then when the button is clicked it transforms left onto the view from outside the view . How can I make it so that when the view loads initially, it is not seen until the button is clicked, using 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)
        })
    }
}

You need to put the UITextField, or any object that you wish to hide, outside of the view by using a function called:

func viewDidLayoutSubviews()

This function is called just before the screen is loaded. It gets the sub-views ready but doesn't show them on the screen yet. So this is an opportunity to hide the UITextField from sight like so:

 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)

}

Now the textfield should be hidden when the view loads and then it can be animated later on.I hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM