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];
        
    
    }
  • 相关阅读:
    BZOJ5302: [Haoi2018]奇怪的背包
    BZOJ5303: [Haoi2018]反色游戏
    UOJ#217. 【UNR #1】奇怪的线段树(广义线段树性质+上下界最小流)
    Codeforces 702F T-Shirts(平衡树+复杂度分析)
    满足决策单调性的 DP 的通用做法
    JZOJ 6754. 2020.07.18【NOI2020】模拟T3 (树链剖分+分治+闵科夫斯基和)
    JZOJ 6756. 2020.07.21【NOI2020】模拟T2 (搜索有用状态+背包dp)
    JZOJ 6757 2020.07.21【NOI2020】模拟T3 (至少容斥+OGF+NTT)
    线性规划转对偶问题
    GDOI 2020 赛前反思
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/5491702.html
Copyright © 2011-2022 走看看