ios - 键盘和文本字段一起动画进入 View

标签 ios uitextfield uikeyboard

我想在 NumberPad 上方有一个文本字段,以便在点击 showKeyboard 时,UIView(带有文本字段和“添加”按钮)+ NumberPad 一起动画到 View 中。我可以让他们分别看到,但不能同时看到。我不确定会发生什么。如果我按一次 showKeyboard,就会出现 numberPad,如果我再次按下 UIView(带有文本字段和“添加”按钮)就会动画化到 View 中。我怎样才能让 numberPad 和 UIView 一起过渡到 View 中? 非常感谢任何帮助!

class ViewController: UIViewController{

    @IBOutlet var showKeyboard: UIButton!
    @IBOutlet var containerView: UIView!
    @IBOutlet var inputField: UITextField!
    @IBOutlet var addData: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func didTapshowKeyboard(sender: AnyObject) {

        inputField.keyboardType = UIKeyboardType.NumberPad
        inputField.becomeFirstResponder()

        let height = containerView.frame.size.height
        let width = containerView.frame.size.width

        //New X Position (same)
        let xPosition = containerView.frame.origin.x // i.e. 0

        //New Y position
        let yPosition = containerView.frame.origin.y - 270

        UIView.animateWithDuration(0.3, animations: {
            self.containerView.frame = CGRect(x: 0, y: yPosition, width: width , height: height) })

        println("tapped")
    }
}

NUmberPad with textfield

最佳答案

您应该在 keyboardWillShow 通知器中进行所有动画计算:(将 _textField 替换为包含您要显示的所有元素的 View 。请注意,一旦您的文本字段成为第一响应者,此方法就会触发)。

- (void)keyboardWillShow:(NSNotification *)notification {
     //this is the time interval for the animation time
    NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSInteger animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    //Get the keyboard frame to calculate your UITextField position
    CGRect keybFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

   //set the textField frame to bottom of screen, before animating it up with the keyboard
   _textField.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, kTextFieldHeight);

   //setting a new frame to the textField
   CGRect frame = CGRectMake(0, (self.view.frame.size.height - keybFrame.size.height) - kTextFieldHeight, self.view.frame.size.width, kTextFieldHeight);

   [UIView animateWithDuration:animationDuration delay:0.0 options:animationCurve animations:^{
        //Making it visible
       _textField.alpha = 1.0;
       _textField.frame = frame;
   } completion:^(BOOL finished) {}];
}

关于ios - 键盘和文本字段一起动画进入 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942668/

相关文章:

ios - Xcode 7 支持 watch OS1 和 OS2

ios - 如何在 Swift 3 + Optional + NSOrderedSet + Generic 中优雅地使用 Core Data

ios - 如何在 swift 2.0 的 UITextfield 中只允许特定的一组数字

ios - 动态更改 UIKeyboards 返回键

ios - 地幔解析数组

ios - 点击 uitextfield 并崩溃

objective-c - 无法让 UITextField 上的“完成”按钮正常工作

ios - 获取当前 iOS 设备在/other/orientation 和隐藏键盘时的键盘高度

iphone - 如何知道 UIkeyboard 是否出现在 iOS 中?

iphone - 更改 UIBarButtonItem 的图像时遇到问题