• 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];

    否则会出错。

    
    
  • 相关阅读:
    U8自动、手动备份不成功 “远程组件初始化失败”
    关于延迟时间的一点智慧
    xe 最大连接数限制、记录客户连接、心跳
    应用开发框架之——根据数据表中的存储的方法名称来调用方法
    固定资产卡片管理累计折旧数不准确
    整理表索引
    用友U8固定资产总账重算语句
    新建自定义报表发布到普通菜单节点流程
    怎么更改月折旧率的小数位
    重建数据库索引等SQL常用语句
  • 原文地址:https://www.cnblogs.com/tyrant2012/p/3892496.html
Copyright © 2020-2023  润新知