zoukankan      html  css  js  c++  java
  • iOS-监听键盘输入,视图总体上移或恢复-避免输入遮挡

    1.初始化输入框,监听输入框開始编辑和结束编辑

    //password输入框
        UITextField *loginInputPasswordStr = [UITextField inputTextWithImage:[UIImage imageNamed:@"Password"] placeholderText:NSLocalizedString(@"Password", nil)];
        loginInputPasswordStr.frame = RectMakeWithPercent(24/375.0, 385/667.0, 327/375.0, 48/667.0);
        //设置键盘类型
        loginInputPasswordStr.keyboardType = UIKeyboardTypeDefault;
        //设置暗文
        [loginInputPasswordStr setSecureTextEntry:YES];
    
        //监听password输入框文字開始输入
        [loginInputPasswordStr addTarget:self action:@selector(loginInputPasswordStrInputBegin) forControlEvents:UIControlEventEditingDidBegin];
    
        //监听password输入框文字结束输入
        [loginInputPasswordStr addTarget:self action:@selector(loginInputPasswordStrInputEnd) forControlEvents:UIControlEventEditingDidEnd];
    
     [self.view addSubview:loginInputPasswordStr];

    2.開始编辑时视图上移,结束编辑或点击屏幕时恢复初始位置.

    //loginInputPasswordStr监听方法
    
    -(void)loginInputPasswordStrInputBegin{
        [self moveView];
    
    }
    
    -(void)loginInputPasswordStrInputEnd{
        [self resumeView];
    }
    
    // 点击屏幕取消输入,恢复视图初始位置
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [self.view endEditing:YES];
        [self resumeView];
    }

    3.主体方法!

    #pragma mark - 点击输入框视图总体上移/恢复
    //恢复原始视图位置
    -(void)resumeView
    {
        NSLog(@"恢复原始视图位置之前self.view.origin.y :%f",self.view.origin.y);
        //当视图上移130像素时->
        if (self.view.frame.origin.y == -130) {
            NSTimeInterval animationDuration=0.40f;
            [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
            [UIView setAnimationDuration:animationDuration];
            //假设当前View是父视图,则Y20个像素高度,假设当前View为其它View的子视图,则动态调节Y的高度
            float fX = self.view.frame.origin.x;
            float fY = self.view.frame.origin.y + 130;
            float width = self.view.frame.size.width;
            float height = self.view.frame.size.height;
            //下移130个单位。按实际情况设置
            self.view.frame = CGRectMake(fX, fY, width, height);
            [UIView commitAnimations];
        }
    }
    
    // 移动窗体
    -(void) moveView
    {
        NSLog(@"移动窗体之前self.view.origin.y :%f",self.view.origin.y);
        //仅仅有当视图在初始位置的时候->
        if (self.view.origin.y == 0) {
            NSTimeInterval animationDuration=0.40f;
            [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
            [UIView setAnimationDuration:animationDuration];
            float fX = self.view.frame.origin.x;
            float fY = self.view.frame.origin.y - 130;
            float width = self.view.frame.size.width;
            float height = self.view.frame.size.height;
            //上移130个单位
            self.view.frame = CGRectMake(fX, fY, width, height);
            [UIView commitAnimations];
        }
    }
  • 相关阅读:
    Windows Server 2003 SP2(32位) 中文版 下载地址 光盘整合方法
    用Recycle()方法对Java对象的重要性
    Lotus中千奇百怪的 $$
    Developing a simple application using steps "User Decision" and "Mail"(1) 沧海
    沟通中的情绪管理(演讲稿) 沧海
    人只有在压力之下,才可能成功,没做一件事,都必须成功,不许言败 沧海
    什么是IDOC,以及IDOC的步骤 沧海
    VS2008 Professional Edition CHS中的deffactory.dat读取错误 沧海
    Including custom text in the step "User Decision" 沧海
    SAP Upgrade Strategy 沧海
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7388279.html
Copyright © 2011-2022 走看看