Obj-C:如何在animateTextView中使用我检索到的键盘高度?

时间:2017-11-23 19:19:46

标签: ios objective-c

我想在我的animateTextView中使用键盘的高度(我在keyboardWillChange'keightHeight'中获取),这样我就可以将它变成运动距离。我怎么能抓住这个价值?看起来它应该很简单,但我似乎无法做到......

ViewController.m

- (void)keyboardWillChange:(NSNotification *)notification {

    CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; 

    CGFloat keyHeight = keyboardRect.size.height;
    NSLog(@"THIS IS THE HEIGHT %f", keyHeight);

}

- (void) animateTextView:(BOOL) up
{

    const int movementDistance = 100;
    const float movementDuration = 0.3f;
    int movement= movement = (up ? -movementDistance : movementDistance);
    NSLog(@"%d",movement);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];

    self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
    [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
    [UIView commitAnimations];

}

1 个答案:

答案 0 :(得分:0)

您可以通过在ViewController实现之前添加私有类来创建私有属性:

@interface ViewController()
@property(nonatomic, assign) CGFloat keyboardHeight;
@end

@implementation ViewController
....

然后在keyboardWillChange:

中为其指定高度值
self.keyboardHeight = keyboardRect.size.height;

然后在animateTextView中使用它:

CGFloat movementDistance = self.keyboardHeight;