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

    否则会出错。

    
    
  • 相关阅读:
    9个免费的桌面产品自动化测试工具
    How to wait for any one of the two element to appear on the screen?
    git仓库过大致使clone失败的解决方法
    Maven项目打包出现:No compiler is provided in this environment. Perhaps you are running on a JRE rather than JDK
    eclipse java maven testng The import org.testng cannot be resolved
    Codeforces Round #165 (Div. 1) B 269B Greenhouse Effect
    Codeforces Round #162 (Div. 1) B 264B Good Sequences
    HDU 4512 HDOJ 吉哥系列故事——完美队形I 腾讯2013初赛第三场
    HDU 4483 HDOJ Lattice triangle
    第二届腾讯校园编程马拉松参赛感想 极限!马拉松
  • 原文地址:https://www.cnblogs.com/tyrant2012/p/3892496.html
Copyright © 2011-2022 走看看