zoukankan      html  css  js  c++  java
  • iOS开发,使用Category实现键盘弹出时,移动View以防被遮住

        嗯,直接上代码!!!!

    这是.h文件的

    #import <UIKit/UIKit.h>
    
    @interface UIView (AboutKeyboard)
    
    @property (nonatomic) CGFloat moveDistince;
    @property (nonatomic) UIView *moveView;
    
    /*
     *指定一个View在键盘出现和消失时移动,如果存在superView则移动superView,否则移动自身
     */
    - (void)registerWhenKeyboardShowAndHidden;
    /*
     *指定一个View在键盘出现和消失时移动,并指定要移动的View
     */
    - (void)registToMoveWhenKeyboardShowAndHideWithView:(UIView *)view;
    /*
     *取消移动
     */
    - (void)removeRegisterWhenKeyboardShowAndHidden;
    
    
    
    @end

    这是.m文件的

    #import "UIView+AboutKeyboard.h"
    #import <objc/runtime.h>
    
    static const void *MoveDistince = &MoveDistince;
    static const void *MoveView = &MoveView;
    
    @implementation UIView (AboutKeyboard)
    
    @dynamic moveDistince;
    @dynamic moveView;
    
    - (void)setMoveDistince:(CGFloat)moveDistince
    {
        objc_setAssociatedObject(self, MoveDistince, @(moveDistince), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (CGFloat)moveDistince
    {
        return [objc_getAssociatedObject(self, MoveDistince) floatValue];
    }
    
    - (void)setMoveView:(UIView *)moveView
    {
        objc_setAssociatedObject(self, MoveView, moveView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIView *)moveView
    {
        return objc_getAssociatedObject(self, MoveView);
    }
    
    - (void)registerWhenKeyboardShowAndHidden
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHidden:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
    - (void)registToMoveWhenKeyboardShowAndHideWithView:(UIView *)view
    {
        self.moveView = view;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHidden:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification
    {
        if (self.isFirstResponder)
        {
            if (!self.moveView)
            {
                if (self.superview)
                    self.moveView = self.superview;//有superView
                else
                    self.moveView = self;//没有superView的情况下移动自身
            }
            NSDictionary *userInfo = notification.userInfo;
            CGRect rect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
            CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
            CGRect willMoveRect = self.moveView.frame;
            CGFloat selfY = CGRectGetMaxY(self.frame);
            CGFloat lastY = CGRectGetHeight(willMoveRect) - CGRectGetHeight(rect);
            if (duration > 0.0f)
            {
                if (selfY > lastY)
                {
                    self.moveDistince = selfY - lastY;//需要移动的距离
                    willMoveRect.origin.y -= self.moveDistince;
                }
            }
            else
            {
                CGFloat oldFloat = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
                CGFloat move = (rect.size.height - oldFloat);
                willMoveRect.origin.y -= move;
                self.moveDistince += move;
            }
            [UIView animateWithDuration:duration animations:^{
                self.superview.frame = willMoveRect;
            }];
        }
    }
    
    - (void)keyboardWillHidden:(NSNotification *)notification
    {
        if (self.isFirstResponder)
        {
            NSDictionary *userInfo = notification.userInfo;
            CGRect willMoveRect = self.moveView.frame;
            willMoveRect.origin.y += self.moveDistince;
            CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
            [UIView animateWithDuration:duration animations:^{
                self.superview.frame = willMoveRect;
            }];
        }
    }
    
    - (void)removeRegisterWhenKeyboardShowAndHidden
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }
    
    @end

    使用方法,以UITextField为例,如有个UITextField *textField,使用方法如下:

    [textFiled registerWhenKeyboardShowAndHidden];

    [textFiled registToMoveWhenKeyboardShowAndHideWithView:textField.superView];

    最后,离开的时候记得

    [textField removeRegisterWhenKeyboardShowAndHidden];

    否则会出错。

    
    
  • 相关阅读:
    【51NOD 1478】括号序列的最长合法子段
    【BZOJ 3527】【ZJOI 2014】力
    【BZOJ 2194】快速傅立叶之二
    【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶
    【BZOJ 2693】jzptab
    【BZOJ 2154】Crash的数字表格
    【BZOJ 3529】【SDOI 2014】数表
    【BZOJ 2820】YY的GCD
    【BZOJ 2301】【HAOI 2011】Problem b
    【POJ 3294】Life Forms 不小于k个字符串中的最长子串
  • 原文地址:https://www.cnblogs.com/tyrant2012/p/3892496.html
Copyright © 2011-2022 走看看