ios tableview 那些事(二十七)tableview 下滑手势关闭键盘


ios 7 简单方法



// 或者代码

self.tableView.keyboardDismissMode  = UIScrollViewKeyboardDismissModeInteractive;




//下面 支持ios 7 一下版本

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>{


    UITextField* textField;

    int originalKeyboardY;

    int originalLocation;

}


@end

<pre name="code" class="objc">

#import "ViewController.h"

#define kFingerGrabHandleHeight     (20.0f)


@interface ViewController (private)

- (void)animateKeyboardReturnToOriginalPosition;

- (void)animateKeyboardOffscreen;

@end


@implementation ViewController


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    

    // always know which keyboard is selected

    // 监听textField是否被选择

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldWasSelected:) name:UITextFieldTextDidBeginEditingNotification object:nil];

    

    // register for when a keyboard pops up

    //监听键盘

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

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

    

    //添加拖动手势

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];

    panRecognizer.delegate = self;

    [self.view addGestureRecognizer:panRecognizer];

}


#pragma mark - Notification Handler


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

    textField = notification.object;

}


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

    // To remove the animation for the keyboard dropping showing

    // we have to hide the keyboard, and on will show we set it back.

    keyboard.hidden = NO;

}


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

    if (keyboard)

        return;

    

    //Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.

    //UIKeyboard is a subclass of UIView anyways

    //see discussion http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html

    /* 遍历子视图*/

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

    for (int i = 0; i < [tempWindow.subviews count]; i++) {

        UIView *possibleKeyboard = [tempWindow.subviews objectAtIndex:i];

        if ([possibleKeyboard isKindOfClass:NSClassFromString(@"UIPeripheralHostView")]) {

            keyboard = possibleKeyboard;

            return;

        }

    }

}


#pragma mark - Gesture Handler


- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer {

    

   


    //触摸当前位置

    CGPoint location = [gestureRecognizer locationInView:[self view]];

    

    /*

      velocityInView

      像素平移速度

      在指定的视图坐标系

     

     */

   // velocity of the pan in pixels/second in the coordinate system of the specified view

    CGPoint velocity = [gestureRecognizer velocityInView:self.view];

    

    

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        //键盘y轴坐标,即高度



        originalKeyboardY = keyboard.frame.origin.y;

   ;

    }

    

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

        // y 轴方向拖动速度

        if (velocity.y > 0) {

        // 执行 键盘下落动画

            [self animateKeyboardOffscreen];

        } else {

            [self animateKeyboardReturnToOriginalPosition];

        }

        

        return;

    }

    

    CGFloat spaceAboveKeyboard = CGRectGetHeight(self.view.bounds) - (CGRectGetHeight(keyboard.frame) + CGRectGetHeight(textField.frame)) + kFingerGrabHandleHeight;

    if (location.y < spaceAboveKeyboard) {

        return;

    }

    

    CGRect newFrame = keyboard.frame;

    

    CGFloat newY = originalKeyboardY + (location.y - spaceAboveKeyboard);

    newY = MAX(newY, originalKeyboardY);

    newFrame.origin.y = newY;

    [keyboard setFrame: newFrame];

    

}


#pragma mark -


- (void)animateKeyboardOffscreen {

    

    [UIView animateWithDuration:0.3

                          delay:0

                        options:UIViewAnimationOptionCurveEaseOut

                     animations:^{

                         CGRect newFrame = keyboard.frame;

                         newFrame.origin.y = keyboard.window.frame.size.height;

                         [keyboard setFrame: newFrame];

                     }

     

                     completion:^(BOOL finished){

                         keyboard.hidden = YES;

                         [textField resignFirstResponder];

                     }];

}


- (void)animateKeyboardReturnToOriginalPosition {

    [UIView beginAnimations:nil context:NULL];

    CGRect newFrame = keyboard.frame;

    newFrame.origin.y = originalKeyboardY;

    [keyboard setFrame: newFrame];

    [UIView commitAnimations];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}


@end



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值