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

    否则会出错。

    
    
  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/tyrant2012/p/3892496.html
Copyright © 2011-2022 走看看