zoukankan      html  css  js  c++  java
  • iOS监听键盘改变view的frame

    将view关联成该类,并把输入框放在该view上.

    //
    //  KeyboardListenerView.m
    //  ParkingProject
    //
    //  Created by Eric on 16/10/12.
    //  Copyright © 2016年 Eric. All rights reserved.
    //
    
    #import "KeyboardListenerView.h"
    
    @implementation KeyboardListenerView
    
    
    //记录键盘是否移动过;
    static bool isMove;
    //记录键盘上移的距离
    static CGFloat subHeight;
    - (void)drawRect:(CGRect)rect {
        // 开始监听
        [self keyBoardNosnotification];
    
    }
    - (void)keyBoardNosnotification {
        //监听键盘状态
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    - (void)keyboardShow:(NSNotification *)notification
    {
        //键盘的frame
        CGRect rect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        //键盘的y
        CGFloat y = rect.origin.y;
        [UIView animateWithDuration:0.2 animations:^{
            //view的最大y值
            CGFloat maxY = CGRectGetMaxY(self.frame);
            //比较view和键盘的y判断是否需要移动
            isMove = maxY > y ? YES:NO;
                if (isMove) {
                    //移动的距离
                    subHeight = maxY-y;
                    //移动,注意不能直接改frame;
                    self.transform = CGAffineTransformMakeTranslation(0, self.transform.ty-subHeight);
                }
        }];
        
    }
    
    - (void)keyboardHide:(NSNotification *)notification
    {
        [UIView animateWithDuration:0.1 animations:^{
            //如果移动了,移动回来.
            if (isMove) {
                self.transform =CGAffineTransformMakeTranslation(0,self.transform.ty+subHeight);
    
            }
        }];
    }
    @end
  • 相关阅读:
    vue项目接入百度地图
    angularJS 十六进制与字符串相互转换
    angular项目实现mqtt的订阅与发布 ngx-mqtt
    消息中间件MQTT
    Zigbee 与 WiFi 的区别
    angular6 路由拼接查询参数如 ?id=1 并获取url参数
    SpringBoot拦截器
    SpringBoot定时任务
    SpringBoot 各层之间的关系
    百度离线地图 —— 瓦片地图下载
  • 原文地址:https://www.cnblogs.com/sunmair/p/5953986.html
Copyright © 2011-2022 走看看