Swift로 사용자 정의 키보드 만들기

12169 단어 Swift

개요


iOS 8의 사용자 정의 키보드에 대한 기사가 많기 때문에.
사용자 정의 키보드를 만들기 전의 절차는 간단하게 써야 한다
나는 사용자 정의 키보드를 만들 때도 내가 좋아하는 곳을 쓰고 싶다.
※ Mac 경험&iOS 시스템은 프로그래밍 경험이 짧기 때문에 보통 일에 푹 빠지기 쉽습니다.
미혹된 점은 다음과 같다.
3
  • 반한 곳 ①: 각환 버튼은 어떻게 하나
  • 3
  • 매혹적인 점 ②: 자작 키보드가 어떻게 반영되는지
  • 3
  • 반한 점 ③: 아까 분명히 자작 키보드가 떴는데...(반함)
  • 개발 환경

  • OS X Mavericks
  • Xcode 6.0.1
  • iOS Simulator
  • 개발 프로세스


    가상 프로젝트 제작


    사용자 정의 키보드 단일 프로젝트가 없습니다...
    가상 Single View Application 프로젝트에서 사용자 정의 키보드를 만들어야 합니다.
  • Xcode -> File -> New -> Project...
  • iOS -> Application -> Single View Application
  • Project에서 Single View Application을 선택합니다.

    프로젝트를 적절하게 설정하다.

    사용자 정의 키보드 타겟 만들기

  • Xcode -> File -> New -> Target...
  • iOS -> Application -> Custom Keyboard
  • Target에서 Custom Keyboard를 선택하십시오...

    목표를 적절하게 설정하다.
    Project 및 Embed in Application
    확인은 가짜 Single View Application입니다.

    완성되면 이런 느낌의 프로젝트 구성입니다.

    Xib 파일 생성하기

  • Xcode -> File -> New -> File...
  • iOS -> User Interface -> View
  • 여느 때처럼 적당하다.
    KeyboardView.먼저 xib라고 불러요.(나중에 프로그램에서 사용)

    Xib 파일에서 버튼 구성



    빠진 곳 ①: 각환 버튼은 어떻게 해요?


    여기는 특별히 안 해도 돼요.
    그냥 하고 싶어서.
    소스에 이렇게 하는 것 같아...
    버튼의 Property를 만들어야 합니까?
    뷰를 원형으로 만들어요.
    [[view layer] setCornerRadius:10.0];
    [view setClipsToBounds:YES];
    
    이런 것들은 많이 조사했고xib에서도 만들어 냈어요.
    User Defined Runtime Attribute에서 layer를 선택합니다.코너 레이디스를 추가합니다.
    Value 수치가 클수록 원의 범위가 커집니다.

    그나저나 이것을 설정해도 미리보기에 반영되지 않는다.
    실행 시에만 반영됩니다.

    UI 및 소스 파일 링크


    Ctrl 키를 사용하여 Buton과 소스를 연결합니다.
    원래 쓴 기본 원본이 거의 사라졌다.
    Keyboard 소스
    class KeyboardViewController: UIInputViewController {
    
        @IBOutlet var nextKeyboardButton: UIButton!
    
        override func updateViewConstraints() {
            super.updateViewConstraints()
    
            // Add custom view sizing constraints here
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            var v = UINib(nibName:"KeyboardView", bundle:nil).instantiateWithOwner(self,options:nil)[0] as UIView
            self.inputView.addSubview(v)
        }
    
        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.
        }
    
        @IBAction func buttonNext(sender: AnyObject) {
            self.advanceToNextInputMode()
        }
    
        @IBAction func button1(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button1")
        }
    
        @IBAction func button2(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button2")
        }
    
        @IBAction func button3(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button3")
        }
    
        @IBAction func button4(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button4")
        }
    
        @IBAction func button5(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button5")
        }
    
        @IBAction func button6(sender: AnyObject) {
            var proxy = textDocumentProxy as UITextDocumentProxy
            proxy.insertText("Button6")
        }
    }
    

    iOS 에뮬레이터로 실행


    미스터리 ②: 자작 키보드는 어떻게 반영합니까?


    적당한 텍스트 상자에 기본 키보드 이외의 내용이 표시되지 않아 고민이다.
    그것은 키보드를 추가하지 않은 것이다...안 나타날 거야...
    Command + Shift + H를 사용하여 에뮬레이터의 홈 페이지로 돌아갑니다.
    Settings -> General -> Keyboard -> Keyboards -> Add New Keyboard... 에 자체 제작 키보드가 추가되었습니다.
    이걸로 키보드를 교환하면 자작 키보드가 표시됩니다.
    실행의 결과는 이렇다.

    ③ 푹 빠져라 ③: 아까 분명히 자작 키보드가 떴는데...(반함)


    갑자기 키보드가 안 나오네.
    아무리 설치해도 나오지 않는다.
    시뮬레이터 측복위는 한 번도 나오지 않는다.
    많은 것을 조사했는데 원인은 하드웨어 키보드인 것 같다.
    텍스트 상자에서도 컴퓨터 키보드로 입력할 수 있는 것이 문제인 것 같다.
    해결 방법:
    Connect Hardware Keyboard->Connect Hardware Keyboard에 대한 검사를 취소합니다.

    기타


    Swift는 문말의 분호가 있거나 없을 수 있다.
    항상 지침과 친하게 지내는 나에게
    분호를 잊고 욕을 먹지 않는 것은 좀 외롭다.

    좋은 웹페이지 즐겨찾기