擷取鍵盤移動動畫,製作控制項跟隨特效。

來源:互聯網
上載者:User

標籤:

此文為轉載。僅供收藏

 

//收到鍵盤彈出通知後的響應

- (void)keyboardWillShow:(NSNotification *)info {

    //儲存info

    NSDictionary *dict = info.userInfo;

    //得到鍵盤的顯示完成後的frame

    CGRect keyboardBounds = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    //得到鍵盤彈齣動畫的時間

    double duration = [dict[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    //座標系轉換

    //由於取出的位置資訊是絕對的,所以要將其轉換為對應於當前view的位置,否則位置資訊會出錯!

    CGRect keyboardBoundsRect = [self.view convertRect:keyboardBounds toView:nil];

    //得到鍵盤的高度,即輸入框需要移動的距離

    double offsetY = keyboardBoundsRect.size.height;

    //得到鍵盤動畫的曲線資訊,按原作的話說“此處是痛點”,stackoverflow網站裡找到的

    UIViewAnimationOptions options = [dict[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;

    //添加動畫

    [UIView animateWithDuration:duration delay:0 options:options animations:^{

        _textField.transform = CGAffineTransformMakeTranslation(0, -offsetY);

    } completion:nil];

   

}

 

//隱藏鍵盤通知的響應

- (void)keyboardWillHide:(NSNotification *)info {

    //輸入框回去的時候就不需要高度了,直接取動畫時間和曲線還原回去

    NSDictionary *dict = info.userInfo;

    double duration = [dict[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationOptions options = [dict[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;

    //CGAffineTransformIdentity是置位,可將改變的transform還原

    [UIView animateWithDuration:duration delay:0 options:options animations:^{

        _textField.transform = CGAffineTransformIdentity;

    } completion:nil];

}

擷取鍵盤移動動畫,製作控制項跟隨特效。

聯繫我們

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