【问题标题】:How to swipe the keyboard along InteractivePopGestureRecognizer?如何沿 InteractivePopGestureRecognizer 滑动键盘?
【发布时间】:2014-01-16 15:38:13
【问题描述】:

我想知道如何使用可见键盘滑动 ViewController?

在 iOS 7 中,我可以左右滑动 ViewController,但键盘保持不动。

简而言之,我想达到以下状态:

谢谢!

【问题讨论】:

    标签: xcode ios7 keyboard uigesturerecognizer pan


    【解决方案1】:

    更新:

    我不能推荐原始解决方案。虽然它表现良好(当它完全表现时),但它是一个不可靠的 hack,并且很容易破坏弹出手势识别器。

    我的同事Dave Lyon 提出了一个很好的解决方案,使用 iOS 7 视图控制器转换并将其打包到一个 pod 中:

    https://github.com/cotap/TAPKeyboardPop

    安装后,只需导入主文件即可。


    原文:

    我很想知道是否有更好的方法来做到这一点,但我能够通过将键盘的视图添加为视图控制器主视图的子视图来实现该行为:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.textView.inputAccessoryView = [UIView new];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self
                   selector:@selector(keyboardWillHide:)
                       name:UIKeyboardWillHideNotification
                     object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)keyboardWillHide:(NSNotification *)note
    {
        if (self.textView.isFirstResponder) {
            UIView *keyboardView = self.textView.inputAccessoryView.superview;
            if (keyboardView) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view addSubview:keyboardView];
                });
            }
        }
    }
    

    我发现您还可以使用手势为键盘设置动画(通过addTarget:action:),但性能很差,如果过早取消手势,则无法清晰地动画。

    【讨论】:

    • 嗨,斯蒂芬。不幸的是 - 这对我来说不是一个好的解决方案。我使用这种方法后,popGesture 立即中断。
    • @TomerRubin 我意识到您必须在 UITextView(或 UITextField)上设置 inputAccessoryView 才能使其正常工作。我已经在一个空白项目中测试了(更新的)上面的代码,它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多