Swift:在自动格式化UITextField后动画UIButton(Swift: Animating UIButton after auto

编程入门 行业动态 更新时间:2024-10-20 16:47:54
Swift:在自动格式化UITextField后动画UIButton(Swift: Animating UIButton after auto-formatting UITextField)

我有一个只接受数字的UITextField 。 这是一个电话号码字段。 一旦字段中有10个字符(区号+数字),我将textField.text字符串转换为NSMutableString将其格式化为电话号码。

现在,一旦格式化,我需要为我的UIButton设置动画,让用户以相同的格式继续下一页,但格式和按钮动画不会同时发生。 我可以键入10个字符,自动格式,退格一个字符,键入另一个数字,按钮将显示,但从不在同一个操作中。

@IBOutlet weak var submitButton: UIButton! @IBOutlet weak var phoneNumber: UITextField! @IBAction func phoneNumberDidChange(sender: AnyObject) { switch count(phoneNumber.text) { case 10: var formattedNumber : NSMutableString = NSMutableString(string: self.phoneNumber.text) formattedNumber.insertString("(", atIndex: 0) formattedNumber.insertString(")", atIndex: 4) formattedNumber.insertString(" ", atIndex: 5) formattedNumber.insertString("-", atIndex: 9) self.phoneNumber.text = formattedNumber as String case 14: UIView.animateWithDuration(0.2, animations: { () -> Void in self.submitButton.center = CGPointMake(self.view.center.x, self.view.center.y) }) default: UIView.animateWithDuration(0.2, animations: { () -> Void in self.submitButton.center = CGPointMake(self.submitButton.center.x - 400, self.submitButton.center.y) }) } } override func viewDidLayoutSubviews() { submitButton.center = CGPointMake(submitButton.center.x - 400, submitButton.center.y) }

如何在同一个动作中获取自动格式和按钮动画? 当我将UIView.animateWithDuration()添加到case 10:根本不会调用它。

I have a UITextField that only takes numbers. It's a phone number field. Once there are 10 characters in the field (area code + number), I convert the textField.text string to an NSMutableString to format it to a phone number.

Now, once the format takes place, I need to animate my UIButton to let the user continue to the next page in the same format, but the format and button animation don't take place at the same time. I can type 10 characters, auto-format, backspace one character, type another number and the button will display, but never in the same action.

@IBOutlet weak var submitButton: UIButton! @IBOutlet weak var phoneNumber: UITextField! @IBAction func phoneNumberDidChange(sender: AnyObject) { switch count(phoneNumber.text) { case 10: var formattedNumber : NSMutableString = NSMutableString(string: self.phoneNumber.text) formattedNumber.insertString("(", atIndex: 0) formattedNumber.insertString(")", atIndex: 4) formattedNumber.insertString(" ", atIndex: 5) formattedNumber.insertString("-", atIndex: 9) self.phoneNumber.text = formattedNumber as String case 14: UIView.animateWithDuration(0.2, animations: { () -> Void in self.submitButton.center = CGPointMake(self.view.center.x, self.view.center.y) }) default: UIView.animateWithDuration(0.2, animations: { () -> Void in self.submitButton.center = CGPointMake(self.submitButton.center.x - 400, self.submitButton.center.y) }) } } override func viewDidLayoutSubviews() { submitButton.center = CGPointMake(submitButton.center.x - 400, submitButton.center.y) }

How can I get the auto-format and button animation in the same action? When I add the UIView.animateWithDuration() to case 10: it doesn't get called at all.

最满意答案

如果你打开了自动布局,你应该这样做,

class ViewController: UIViewController { @IBOutlet weak var submitButton: UIButton! @IBOutlet weak var phoneNumber: UITextField! @IBOutlet weak var centerCon: NSLayoutConstraint! // IBOutlet to a centerX constraint on the button override func viewDidLoad() { centerCon.constant = self.view.frame.size.width/2 + submitButton.frame.size.width // puts the button just off screen to the left } @IBAction func phoneNumberDidChange(sender: AnyObject) { switch count(phoneNumber.text) { case 10: var formattedNumber : NSMutableString = NSMutableString(string: self.phoneNumber.text) formattedNumber.insertString("(", atIndex: 0) formattedNumber.insertString(")", atIndex: 4) formattedNumber.insertString(" ", atIndex: 5) formattedNumber.insertString("-", atIndex: 9) self.phoneNumber.text = formattedNumber as String self.centerCon.constant = 0 UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) case 14 self.centerCon.constant = 0 UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) default: centerCon.constant = self.view.frame.size.width/2 + submitButton.frame.size.width UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) } } }

你的案例14永远不会被调用,因为在代码中格式化字符串不会调用你的phoneNumberDidChange:function(只有文本字段中的用户条目才会调用它)。 在上面的代码中,请注意在两种情况下调用动画(因此它在格式化之后发生),并且在情况14中也是如此(如果您返回空格然后添加更多数字)。

If you have auto layout turned on, you should do it something like this,

class ViewController: UIViewController { @IBOutlet weak var submitButton: UIButton! @IBOutlet weak var phoneNumber: UITextField! @IBOutlet weak var centerCon: NSLayoutConstraint! // IBOutlet to a centerX constraint on the button override func viewDidLoad() { centerCon.constant = self.view.frame.size.width/2 + submitButton.frame.size.width // puts the button just off screen to the left } @IBAction func phoneNumberDidChange(sender: AnyObject) { switch count(phoneNumber.text) { case 10: var formattedNumber : NSMutableString = NSMutableString(string: self.phoneNumber.text) formattedNumber.insertString("(", atIndex: 0) formattedNumber.insertString(")", atIndex: 4) formattedNumber.insertString(" ", atIndex: 5) formattedNumber.insertString("-", atIndex: 9) self.phoneNumber.text = formattedNumber as String self.centerCon.constant = 0 UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) case 14 self.centerCon.constant = 0 UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) default: centerCon.constant = self.view.frame.size.width/2 + submitButton.frame.size.width UIView.animateWithDuration(0.2, animations: { () -> Void in self.view.layoutIfNeeded() }) } } }

Your case 14 never gets called, because formatting the string in code doesn't call your phoneNumberDidChange: function (only user entry in the text field calls it). In the code above, notice that the animation is called in both case 10 (so it happens right after the formatting), and also in case 14 (in case you back space and then add more numbers).

更多推荐

本文发布于:2023-08-06 14:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   UITextField   Swift   auto   Animating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!