【问题标题】:Show/Hide keyboard when swipe on screen like imessenger provides像 imessenger 一样在屏幕上滑动时显示/隐藏键盘
【发布时间】:2014-01-23 12:11:13
【问题描述】:

我想实现,当从屏幕底部滑动到屏幕顶部时,键盘会显示在屏幕上,而当在屏幕上从顶部到底部滑动时,键盘会隐藏。 当我们在屏幕上滑动搜索文本字段和键盘时,它就像 iOS 7 的效果,当我们向下滑动它的隐藏时。

【问题讨论】:

    标签: ios ios7 keyboard swipe


    【解决方案1】:

    试试这个:

    //declare a property to store your current responder
    @property (nonatomic, assign) id currentResponder;
    //in viewDidLoad:
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnSwipe:)];
    [self.collectionView addGestureRecognizer:swipe];
    
    
    //Implement the below delegate method:
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        self.currentResponder = textField;
    }
    
    //Implement resignOnSwipe:
    
    - (void)resignOnSwipe:(id)sender {
        [self.currentResponder resignFirstResponder]
    }
    

    【讨论】:

      【解决方案2】:

      试试这样的东西,..它工作正常

      在xib中,

      添加一个文本字段并将其隐藏。将其连接到您的 .h 文件并将其命名为 tf。

      在.h文件中,

      添加

      #import <UIKit/UIKit.h>
      
      @interface KeyboardDisplay : UIViewController <UIGestureRecognizerDelegate>
      {    
      

      __weak IBOutlet UITextField *tf;

      }
      

      在.m文件中,

      - (void)viewDidLoad
      {
      
      [super viewDidLoad];
      
      UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
      swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
      [self.view addGestureRecognizer:swipeGesture];
      
      
      UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
      swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
      [self.view addGestureRecognizer:swipeGesture2];
      
      }
      
      -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
      {
      
      //Gesture detect - swipe up/down , can be recognized direction
      if(sender.direction == UISwipeGestureRecognizerDirectionUp)
      {
      
          [tf becomeFirstResponder];
      
          NSLog(@"Up");
      
      }
      else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
      {
      
          [tf resignFirstResponder];
          NSLog(@"down");
      }
      }
      

      注意:不要忘记在 xib 中隐藏文本字段。

      【讨论】:

        猜你喜欢
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 2022-06-24
        相关资源
        最近更新 更多