2014-10-02 229 views
2

我已經用這種方式構建鍵盤。 (下面的代碼)iOS的自定義鍵盤

但是,有喜歡的一些問題:

  • 當你點擊一些按鈕,它的點擊動畫長走回去。
  • 沒有辦法來放置一些常用的按鍵作爲全球符號語言改變或大寫鎖定

我想要做的是,要修改原來的iOS鍵盤,並添加一些其他按鈕。

可能嗎?有什麼建議麼?

import UIKit 

class KeyboardViewController: UIInputViewController { 

    @IBOutlet var nextKeyboardButton: UIButton! 

    override func updateViewConstraints() { 
     super.updateViewConstraints() 

     // Add custom view sizing constraints here 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let buttonTitles1 = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"] 
     let buttonTitles2 = ["A", "S", "D", "F", "G", "H", "J", "K", "L"] 
     let buttonTitles3 = ["CP", "Z", "X", "C", "V", "B", "N", "M", "BP"] 
     let buttonTitles4 = ["CHG", "SPACE", "RETURN"] 

     var row1 = createRowOfButtons(buttonTitles1) 
     var row2 = createRowOfButtons(buttonTitles2) 
     var row3 = createRowOfButtons(buttonTitles3) 
     var row4 = createRowOfButtons(buttonTitles4) 

     self.view.addSubview(row1) 
     self.view.addSubview(row2) 
     self.view.addSubview(row3) 
     self.view.addSubview(row4) 

     row1.setTranslatesAutoresizingMaskIntoConstraints(false) 
     row2.setTranslatesAutoresizingMaskIntoConstraints(false) 
     row3.setTranslatesAutoresizingMaskIntoConstraints(false) 
     row4.setTranslatesAutoresizingMaskIntoConstraints(false) 

     addConstraintsToInputView(self.view, rowViews: [row1, row2, row3, row4]) 
    } 

    func createRowOfButtons(buttonTitles: [NSString]) -> UIView { 

     var buttons = [UIButton]() 
     var keyboardRowView = UIView(frame: CGRectMake(0, 0, 320, 50)) 

     for buttonTitle in buttonTitles{ 

      let button = createButtonWithTitle(buttonTitle) 
      buttons.append(button) 
      keyboardRowView.addSubview(button) 
     } 

     addIndividualButtonConstraints(buttons, mainView: keyboardRowView) 

     return keyboardRowView 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated 
    } 

    override func textWillChange(textInput: UITextInput) { 
     // The app is about to change the document's contents. Perform any preparation here. 
    } 

    override func textDidChange(textInput: UITextInput) { 
     // The app has just changed the document's contents, the document context has been updated. 

     var textColor: UIColor 
     var proxy = self.textDocumentProxy as UITextDocumentProxy 
     if proxy.keyboardAppearance == UIKeyboardAppearance.Dark { 
      textColor = UIColor.whiteColor() 
     } else { 
      textColor = UIColor.blackColor() 
     } 
    } 



    func createButtonWithTitle(title: String) -> UIButton { 

     let button = UIButton.buttonWithType(.System) as UIButton 
     button.frame = CGRectMake(0, 0, 20, 20) 
     button.setTitle(title, forState: .Normal) 
     button.sizeToFit() 
     button.titleLabel?.font = UIFont.systemFontOfSize(15) 
     button.setTranslatesAutoresizingMaskIntoConstraints(false) 
     button.backgroundColor = UIColor(white: 1.0, alpha: 1.0) 
     button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal) 

     button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside) 

     return button 
    } 

    func didTapButton(sender: AnyObject?) { 

     let button = sender as UIButton 
     var proxy = textDocumentProxy as UITextDocumentProxy 

     if let title = button.titleForState(.Normal) { 
      switch title { 
      case "BP" : 
       proxy.deleteBackward() 
      case "RETURN" : 
       proxy.insertText("\n") 
      case "SPACE" : 
       proxy.insertText(" ") 
      case "CHG" : 
       self.advanceToNextInputMode() 
      default : 
       proxy.insertText(title) 
      } 
     } 
    } 

    func addIndividualButtonConstraints(buttons: [UIButton], mainView: UIView){ 

     for (index, button) in enumerate(buttons) { 

      var topConstraint = NSLayoutConstraint(item: button, attribute: .Top, relatedBy: .Equal, toItem: mainView, attribute: .Top, multiplier: 1.0, constant: 1) 

      var bottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: mainView, attribute: .Bottom, multiplier: 1.0, constant: -1) 

      var rightConstraint : NSLayoutConstraint! 

      if index == buttons.count - 1 { 

       rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: mainView, attribute: .Right, multiplier: 1.0, constant: -1) 

      }else{ 

       let nextButton = buttons[index+1] 
       rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: nextButton, attribute: .Left, multiplier: 1.0, constant: -1) 
      } 


      var leftConstraint : NSLayoutConstraint! 

      if index == 0 { 

       leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: mainView, attribute: .Left, multiplier: 1.0, constant: 1) 

      }else{ 

       let prevtButton = buttons[index-1] 
       leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: prevtButton, attribute: .Right, multiplier: 1.0, constant: 1) 

       let firstButton = buttons[0] 
       var widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .Width, relatedBy: .Equal, toItem: button, attribute: .Width, multiplier: 1.0, constant: 0) 

       widthConstraint.priority = 800 
       mainView.addConstraint(widthConstraint) 
      } 

      mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint]) 
     } 
    } 


    func addConstraintsToInputView(inputView: UIView, rowViews: [UIView]){ 

     for (index, rowView) in enumerate(rowViews) { 
      var rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .Right, relatedBy: .Equal, toItem: inputView, attribute: .Right, multiplier: 1.0, constant: -1) 

      var leftConstraint = NSLayoutConstraint(item: rowView, attribute: .Left, relatedBy: .Equal, toItem: inputView, attribute: .Left, multiplier: 1.0, constant: 1) 

      inputView.addConstraints([leftConstraint, rightSideConstraint]) 

      var topConstraint: NSLayoutConstraint 

      if index == 0 { 
       topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: inputView, attribute: .Top, multiplier: 1.0, constant: 0) 

      }else{ 

       let prevRow = rowViews[index-1] 
       topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: prevRow, attribute: .Bottom, multiplier: 1.0, constant: 0) 

       let firstRow = rowViews[0] 
       var heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .Height, relatedBy: .Equal, toItem: rowView, attribute: .Height, multiplier: 1.0, constant: 0) 

       heightConstraint.priority = 800 
       inputView.addConstraint(heightConstraint) 
      } 
      inputView.addConstraint(topConstraint) 

      var bottomConstraint: NSLayoutConstraint 

      if index == rowViews.count - 1 { 
       bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: inputView, attribute: .Bottom, multiplier: 1.0, constant: 0) 

      }else{ 

       let nextRow = rowViews[index+1] 
       bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: nextRow, attribute: .Top, multiplier: 1.0, constant: 0) 
      } 

      inputView.addConstraint(bottomConstraint) 
     } 

    } 
} 

回答

3

當你點擊某個按鈕時,它是點擊動畫需要很長時間才能找回來的。

您的鑰匙無非是UIButton s。您看到的淡出動畫是UIButton的正常動畫 - 您可以在整個系統的大多數按鈕上看到自己的動畫。不要使用UIButtonType.System對其進行初始化,而應使用.Custom併爲所需的狀態設置您自己的外觀。

爲例(有很多可能性):

let button = UIButton(.Custom) 
button.setTitleColor(UIColor.redColor(), forState: .Highlighted) 

注意,你甚至不用給自己限制使用UIButton -custom鍵盤是一片空白。

有沒有辦法來放置一些常用的按鍵作爲全球符號語言改變或大寫鎖定

Custom Keyboard Guide

系統挑選合適的「下一個」鍵盤;有沒有API 獲得啓用的鍵盤列表或選擇一個特定的 鍵盤切換到。

因此,這些「特殊」鍵也由您自己定製圖標提供給您。如果您使用UIButton,那可能意味着撥打setImage:forState:。看起來大多數第三方鍵盤使用的地球圖標幾乎與系統的「下一個」鍵相同。

沒有API來修改系統鍵盤 - 您必須從頭開始自己構建一個。

2

對於緩慢的動畫,你可以學習他們如何做自己的動畫,這些GitHub庫:

  1. https://github.com/YuAo/WUEmoticonsKeyboard(見popview當按鈕被按下)
  2. https://github.com/ayushgoel/AGEmojiKeyboard
  3. https://github.com/kulpreetchilana/Custom-iOS-Keyboards
  4. 或搜索github:https://github.com/search?utf8 =✓& q = iOS +鍵盤

注意:它們大多是objective-c回購。

最後,它可能更便宜安裝自定義鍵盤比作一個: http://www.imore.com/best-custom-keyboards-ios-8