您的位置:首页 > 移动开发 > IOS开发

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

2014-07-30 18:36 387 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios