ios在數字鍵台左下角添加“完成”按鈕的實現原理

來源:互聯網
上載者:User

標籤:ios開發   ios   ios數字鍵台添加完成按鈕   

        最近要在系統彈出的數字鍵台上的左下角額外添加一個自定製的完成按鈕,於是研究了一下系統內建鍵盤添加自定製按鈕的實現方式。總結了一下大體上的通用做法,原理大概是這樣:當頁面上的文字框或其他輸入源因為使用者的點擊而變成第一響應者的時候(becomeFirstResponder),系統鍵盤就會彈出。而每次鍵盤彈出或收合時,都會向系統發送相關的鍵盤事件即通知訊息(notification)。所以,我們只要在鍵盤彈出或收合時捕獲相關的鍵盤事件,並且在鍵盤對應的window上的相應位置添加或移除我們自定製的按鈕即可。

      按照這種思路,在鍵盤彈出時添加上我們自定製的按鈕是完全可行的。但是,如果只是這樣簡單的處理,造成的效果是:鍵盤彈出和自定製按鈕的添加是不同步的。有時候鍵盤還沒有彈出自定製的按鈕就已經加在試圖上了,有時候鍵盤彈出後自定製的按鈕才添加上去。那麼我們如何使得鍵盤的彈出和自定製按鈕的添加能同步進行呢,要完成這種平滑的過度彈出效果,還需要獲得鍵盤彈出的動畫時間和動畫類型,然後讓自定製按鈕的添加動畫和鍵盤的彈出、收合動作同步。


1、首先在 viewWillAppear 方法中註冊監聽相應的鍵盤通知,並且要在 viewWillDisappear 方法中登出通知

- (void)viewWillAppear:(BOOL)animated {        //註冊鍵盤顯示通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];    //註冊鍵盤隱藏通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];        [super viewWillAppear:animated];}

-(void)viewWillDisappear:(BOOL)animated{        //登出鍵盤顯示通知    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];        [super viewWillDisappear:animated];}


2、處理鍵盤彈出和收合事件

// 鍵盤出現處理事件- (void)handleKeyboardDidShow:(NSNotification *)notification{    // NSNotification中的 userInfo字典中包含鍵盤的位置和大小等資訊    NSDictionary *userInfo = [notification userInfo];    // UIKeyboardAnimationDurationUserInfoKey 對應鍵盤彈出的動畫時間    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];    // UIKeyboardAnimationCurveUserInfoKey 對應鍵盤彈出的動畫類型    NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];    //數字彩,數字鍵台添加“完成”按鈕    if (doneInKeyboardButton){                [UIView beginAnimations:nil context:NULL];        [UIView setAnimationDuration:animationDuration];//設定添加按鈕的動畫時間        [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];//設定添加按鈕的動畫類型                //設定自定製按鈕的添加位置(這裡為數字鍵台添加“完成”按鈕)        doneInKeyboardButton.transform=CGAffineTransformTranslate(doneInKeyboardButton.transform, 0, -53);                [UIView commitAnimations];    }    }

// 鍵盤消失處理事件- (void)handleKeyboardWillHide:(NSNotification *)notification{    // NSNotification中的 userInfo字典中包含鍵盤的位置和大小等資訊    NSDictionary *userInfo = [notification userInfo];    // UIKeyboardAnimationDurationUserInfoKey 對應鍵盤收合的動畫時間    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];    if (doneInKeyboardButton.superview)    {        [UIView animateWithDuration:animationDuration animations:^{            //動畫內容,將自定製按鈕移回初始位置            doneInKeyboardButton.transform=CGAffineTransformIdentity;        } completion:^(BOOL finished) {            //動畫結束後移除自定製的按鈕            [doneInKeyboardButton removeFromSuperview];        }];            }}


3、點擊輸入框,初始化自定製按鈕並彈出鍵盤

//點擊輸入框- (IBAction)editingDidBegin:(id)sender{        //初始化數字鍵台的“完成”按鈕    [self configDoneInKeyBoardButton];}

//初始化,數字鍵台“完成”按鈕- (void)configDoneInKeyBoardButton{    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;    //初始化    if (doneInKeyboardButton == nil)    {        doneInKeyboardButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];        [doneInKeyboardButton setTitle:@"完成" forState:UIControlStateNormal];        [doneInKeyboardButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];                doneInKeyboardButton.frame = CGRectMake(0, screenHeight, 106, 53);                doneInKeyboardButton.adjustsImageWhenHighlighted = NO;        [doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];    }    //每次必須從新設定“完成”按鈕的初始化座標位置    doneInKeyboardButton.frame = CGRectMake(0, screenHeight, 106, 53);        //由於ios8下,鍵盤所在的window視圖還沒有初始化完成,調用在下一次 runloop 下獲得鍵盤所在的window視圖    [self performSelector:@selector(addDoneButton) withObject:nil afterDelay:0.0f];    }- (void) addDoneButton{    //獲得鍵盤所在的window視圖    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];    [tempWindow addSubview:doneInKeyboardButton];    // 注意這裡直接加到window上    }

//點擊“完成”按鈕事件,收合鍵盤-(void)finishAction{    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//關閉鍵盤}
         

       按照這種思路和實現原理,可以在鍵盤上方等地方添加輸入框等其他自定製控制項並且可以跟隨鍵盤的彈出和收合平滑過度出現。

ios在數字鍵台左下角添加“完成”按鈕的實現原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.