閱讀734 返回首頁    go 阿裏雲 go 技術社區[雲棲]


定製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


最後更新:2017-04-03 05:39:42

  上一篇:go NSString加密相關擴展
  下一篇:go HDU1548-A strange lift【廣搜做法】