定制scrollView来处理自动显示与隐藏键盘

//
//  HYBKeyboardScrollView.h
//  HomeLinkProject
//
//  Created by huangyibiao on 14-6-3.
//  Copyright (c) 2014年 huangyibiao. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol HYBKeyboardScrollViewDelegate <NSObject>

- (void)keyboardWillHide;

@end

/*!
 * @brief 继承于UIScrollView,添加解决键盘自动隐藏的功能
 * @author huangyibiao
 */
@interface HYBKeyboardScrollView : UIScrollView

// 键盘将要键盘的代理
@property (nonatomic, weak) id<HYBKeyboardScrollViewDelegate> keyboardHideDelegate;

@end
//
//  HYBKeyboardScrollView.m
//  HomeLinkProject
//
//  Created by huangyibiao on 14-6-3.
//  Copyright (c) 2014年 huangyibiao. All rights reserved.
//

#import "HYBKeyboardScrollView.h"

@interface HYBKeyboardScrollView ()

/*!
 * @brief 上一次的偏移量
 */
@property(nonatomic, assign) CGPoint previousOffset;

// 添加、移除对键盘的监听通知
- (void)addKeyboardNotifications;
- (void)removeKeyboardNotifications;

// 键盘出现、隐藏的通知回调
- (void)keyboardWillShow:(NSNotification *)notification;
- (void)keyboardWillHide:(NSNotification *)notification;
@end

@implementation HYBKeyboardScrollView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addKeyboardNotifications];
    }
    return self;
}

- (void)awakeFromNib {
    [self addKeyboardNotifications];
    self.contentSize = CGSizeMake(320, 700);
    return;
}

- (void)dealloc {
    [self removeKeyboardNotifications];
    return;
}

- (void)addKeyboardNotifications {
    [kNotificationCenter addObserver:self
                            selector:@selector(keyboardWillShow:)
                                name:UIKeyboardWillShowNotification
                              object:nil];
    [kNotificationCenter addObserver:self
                            selector:@selector(keyboardWillHide:)
                                name:UIKeyboardWillHideNotification
                              object:nil];
    return;
}

- (void)removeKeyboardNotifications {
    [kNotificationCenter removeObserver:self
                                name:UIKeyboardWillShowNotification
                              object:nil];
    [kNotificationCenter removeObserver:self
                                name:UIKeyboardWillHideNotification
                              object:nil];
    return;
}

// 点击滚动视图时隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self endEditing:YES];
    if ([self.keyboardHideDelegate respondsToSelector:@selector(keyboardWillHide)]) {
        [self.keyboardHideDelegate keyboardWillHide];
    }
    return;
}

// scroll contentOffset when keybord will show
- (void)keyboardWillShow:(NSNotification *)notification {
    self.previousOffset = self.contentOffset;
    NSDictionary *userInfo = [notification userInfo];

    // get keyboard rect in windwo coordinate
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // convert keyboard rect from window coordinate to scroll view coordinate
    keyboardRect = [self convertRect:keyboardRect fromView:nil];
    // get keybord anmation duration
    NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // get first responder textfield
    UIView *currentResponder = [self findFirstResponderBeneathView:self];
    if (currentResponder != nil) {
        // convert textfield left bottom point to scroll view coordinate
        CGPoint point = [currentResponder convertPoint:CGPointMake(0, currentResponder.frame.size.height) toView:self];
        // 计算textfield左下角和键盘上面20像素 之间是不是差值
        float scrollY = point.y - (keyboardRect.origin.y - 20);
        if (scrollY > 0) {
            [UIView animateWithDuration:animationDuration animations:^{
                //移动textfield到键盘上面20个像素
                self.contentOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + scrollY);
            }];
        }
    }
    self.scrollEnabled = NO;
    return;
}

// roll back content offset
- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:animationDuration animations:^{
        self.contentOffset = self.previousOffset;
    }];
    self.scrollEnabled = YES;
    if ([self.keyboardHideDelegate respondsToSelector:@selector(keyboardWillHide)]) {
        [self.keyboardHideDelegate keyboardWillHide];
    }
    return;
}

- (UIView *)findFirstResponderBeneathView:(UIView *)view {
    // 递归查找第一响应者
    for (UIView *childView in view.subviews ) {
        if ([childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) {
            return childView;
        }
        UIView *result = [self findFirstResponderBeneathView:childView];
        if (result) {
            return result;
        }
    }
    return nil;
}

@end
时间: 2024-10-24 15:58:31

定制scrollView来处理自动显示与隐藏键盘的相关文章

jQuery实现根据类型自动显示和隐藏表单

 这篇文章主要给大家分享了jQuery实现根据类型自动显示和隐藏表单的代码,非常的简单实用,仅仅10行代码,推荐给大家,希望能给大家一些提示.     jquery实现表单根据单选按钮进行字段的动画切换,昨天写的,感觉比起初学时写的js/jquery有了很大进步..在最大化扩展性的情况下经可能使代码精简. html   代码如下: <div class="control-group"> <label class="control-label">

jQuery实现根据类型自动显示和隐藏表单_jquery

jquery实现表单根据单选按钮进行字段的动画切换,昨天写的,感觉比起初学时写的js/jquery有了很大进步..在最大化扩展性的情况下经可能使代码精简. html 复制代码 代码如下: <div class="control-group">                     <label class="control-label">类型:</label>                     <div class=

Android 检测键盘显示或隐藏键盘的实现代码

Android 检测键盘是否显示及隐藏键盘的方法~~ Android 中对于键盘的隐藏与显示的文章很多,今天公司项目需求不仅需要键盘隐藏和显示还需要对键盘的状态进行检查,这里做一个简单的实现实例,供大家参考: 实现代码: package com.newland.util; import android.app.Activity; import android.view.View; import android.view.inputmethod.InputMethodManager; /** *

滚动时自动显示与隐藏导航条定制

// // AMScrollingNavbarViewController.h // AMScrollingNavbar // // Created by Andrea on 08/11/13. // Copyright (c) 2013 Andrea Mazzini. All rights reserved. // #import <UIKit/UIKit.h> /*! * @name HYBScrollRenderController * @brief 在滚动视图向上滚动时,慢慢隐藏导航条

OC滚动时自动显示与隐藏导航条定制

问题描述 本帖最后由 行走的鼠标 于 2015-3-13 08:50 编辑 // // AMScrollingNavbarViewController.h // AMScrollingNavbar // // Created by Andrea on 08/11/13. // Copyright (c) 2013 Andrea Mazzini. All rights reserved. // #import /*! * @name HYBScrollRenderController * @bri

Word2010如何实现鼠标指向自动显示提示文字

在对Word文档进行操作时,经常需要给一些关键地方添加提示文字,但又不能让提示影响到原文的视图和打印效果.因此想让提示文字只在鼠标靠近时自动显示,当鼠标移开时就得自动消失.请问在Word中是否有办法可实现类似的效果呢? 在Word2010中有两种方法可以达到你要求的鼠标指向自动显示和隐藏的效果:使用超链接和尾注,他们各有优劣,小编来教你使用这两种方法. 一.超链接 在Word2010文档中,当鼠标指针指向超链接时默认将显示链接地址.不过用户可以根据需要自定义超链接屏幕提示文字,操作步骤如下所述:

神奇的listView实现自动显示隐藏布局Android代码_Android

借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示隐藏Toolbar 首先给listView增加一个HeaderView,避免第一个Item被Toolbar遮挡.  View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView

Android ListView自动显示隐藏布局的实现方法_Android

借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示隐藏Toolbar 首先给listView增加一个HeaderView,避免第一个Item被Toolbar遮挡. View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.

Android ListView自动显示隐藏布局的实现方法

借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示隐藏Toolbar 首先给listView增加一个HeaderView,避免第一个Item被Toolbar遮挡. View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.