iOS 7后退滑动手势和键盘解雇

时间:2014-07-20 13:46:48

标签: cocoa-touch ios7 uinavigationcontroller uikit uikeyboard

对于iOS 7,我有一个标准UINavigationViewController标准UIInteractivePopGestureRecognizer。我也有这些众所周知且广泛使用的方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

他们的工作方式如下:

- (void)keyboardWillShow:(NSNotification *)notification{
    CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

    if (keyboardIsUp || isAnimating) return;

    isAnimating = YES;

    [UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        _topConstraint.constant -= kTopConstraintR4Ratio;
        [self.view layoutIfNeeded];
    }completion:^(BOOL finished){
        keyboardIsUp = YES;
        isAnimating = NO;
    }];
}

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

    CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

    if (isAnimating) return;

    isAnimating = YES;

    [UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        _topConstraint.constant = topConstraintOriginalValue;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        keyboardIsUp = NO;
        isAnimating = NO;
    }];
}

当我用手势向后滑动视图时,UIView animateWithDuration被触发而没有任何持续时间。整个视图变成一团糟 - 当我弹出视图时它会慢慢动画,但是当手势没有完成时 - 视图会跳起来。

看起来像这样:

enter image description here

如何摆脱这种奇怪的行为? 我不希望视图在转换期间移动,我希望它保持不动。

修改

您可以使用以下链接下载示例项目:

https://www.dropbox.com/s/034lfy49ru4pelf/SampleProject.zip

2 个答案:

答案 0 :(得分:2)

轻松破解:

BOOL needAdjustContstrain;

needAdjustContstrain

中将YES设为keyboardWillShow:
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (needAdjustContstrain) {
         _topContstraint.constant -=40;
    }
}

答案 1 :(得分:-2)

您需要在动画块之前调用layoutIfneeded方法。看看下面的代码:

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

CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

if (isAnimating) return;

isAnimating = YES;

[self.view layoutIfNeeded];
[UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
    _topContstraint.constant = 89;
  //
} completion:^(BOOL finished) {
    keyboardIsUp = NO;
    isAnimating = NO;
}];}

请查看link