gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:14:11 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com