UI鍵盤通知,ui鍵盤

來源:互聯網
上載者:User

UI鍵盤通知,ui鍵盤

 

 

#import "ViewController.h"

 

@interface ViewController ()

@property (nonatomic,strong) UITextField *tf;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _tf = [[UITextField alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height - 40, 300, 30)];

    _tf.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_tf];

    

    /*

    通知中樞(單例)

    通知中樞是一對多的,即同一個廣播可以被多個收音機接收

     代理是一對一

     作用:1.用來接收廣播和發起廣播

                 2.用通知的名字作為頻道

     */

    /*

    參數一:響應的類

     參數二:類中響應的方法

     參數三:通知的名字(即頻道)

     參數四:接收類型 [注意]nil代表任何類型

     */

    //:UIKeyboardWillShowNotification接收鍵盤將要顯示的通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

    

    //UIKeyboardWillHideNotification接收鍵盤將要隱藏的通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    

}

#pragma mark - UIKeyboardWillHideNotification

//鍵盤將要隱藏

-(void)keyBoardWillHide:(NSNotification *)noti

{

     //noti.userInfo是一個字典,大家可以輸出來看看字典裡麵包含了什麼

    //NSLog(@"%@",noti.userInfo);

 

    //獲得彈下去後的座標[注意是彈下後的]

    CGRect keyBoardEndFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //NSLog(@"--%@",NSStringFromCGRect(keyBoardEndFrame));

    [UIView animateWithDuration:2 animations:^{

        //改變文字框的位置,讓它跟著鍵盤一起彈起來

        CGRect tfRext = _tf.frame;

        tfRext.origin.y = keyBoardEndFrame.origin.y-_tf.bounds.size.height-10;

        _tf.frame = tfRext;

    }];

}

 

#pragma mark - UIKeyBoard notification

//接收到的是通知,所以參數用NSNotification

//鍵盤將要顯示

-(void)keyBoardWillShow:(NSNotification *)noti

{

    CGRect keyBoardFrame = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //可以通過以下方法輸出CGRect

    //NSStringFromCGRect將CGRect轉化為字串的方式擷取Frame

    //NSLog(@"%@",NSStringFromCGRect(keyBoardFrame));

    

    //擷取鍵盤的動畫期間

    //CGFloat keyBoardDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];

    

    [UIView animateWithDuration:2 animations:^{

        CGRect tfRect = _tf.frame;

        tfRect.origin.y = keyBoardFrame.origin.y - _tf.bounds.size.height -10;

        _tf.frame = tfRect;

    }];

    

}

#pragma mark - 當手指觸碰螢幕任何一地方時被調用

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //利用結束編輯來隱藏鍵盤

    [self.view endEditing:YES];

}

 

 

@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.