zoukankan      html  css  js  c++  java
  • iOS keyBoardDemo

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, retain) UIView *aView; /**< 键盘背地图 */
    @property (nonatomic, retain) UITextField *textField; /**< 键盘 */
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor orangeColor];
        
        [self initTextField];
    }
    
    #pragma mark 键盘演示
    /**键盘演示 */
    - (void)initTextField
    {
        self.aView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40)];
        self.aView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:self.aView];
    
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2,5,250,30)];
        self.textField.backgroundColor = [UIColor whiteColor];
        self.textField.placeholder = @"请输入";
        [self.aView addSubview:self.textField];
        
        
        //UIKeyboardWillShow
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardwillAppear:) name:UIKeyboardWillShowNotification object:nil];
        //UIKeyboardWillHide
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    /** UIKeyboardWillHide */
    - (void)UIKeyboardWillHide:(NSNotification *)notifation
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.aView.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
        }];
    }
    
    
    /** keyBoardwillAppear */
    - (void)keyBoardwillAppear:(NSNotification *)notifation
    {
        NSLog(@"%@",notifation);
        CGRect KeyboardFrame = [[notifation.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
        NSLog(@"%@",NSStringFromCGRect(KeyboardFrame));
        
        //UIView动画
        [UIView animateWithDuration:0.25 animations:^{
            self.aView.frame = CGRectMake(0 , self.view.frame.size.height - KeyboardFrame.size.height -50, self.view.frame.size.width, 50);
        }];
    }
    
    /** 触摸开始的时候回收键盘 */
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self.textField resignFirstResponder];
    }
    
    
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:UIKeyboardWillShowNotification];
        [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:UIKeyboardWillHideNotification];
        
    
    }
  • 相关阅读:
    关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
    【设计模式】抽象工厂模式
    spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
    struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
    git中Please enter a commit message to explain why this merge is necessary.
    扒一扒开源世界有哪些licenses?
    string.Format出现异常:输入字符串的格式不正确 Exception during StringFormat
    node-glob学习
    js中对URL进行转码与解码
    程序员如何修炼管理思维
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/5491702.html
Copyright © 2011-2022 走看看