|
| 1 | +// |
| 2 | +// ICKeyboardDismissTextField.swift |
| 3 | +// ICInputAccessory |
| 4 | +// |
| 5 | +// Created by Ben on 07/03/2016. |
| 6 | +// Copyright © 2016 Polydice, Inc. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +import UIKit |
| 28 | + |
| 29 | +public class ICKeyboardDismissTextField: UITextField { |
| 30 | + |
| 31 | + private lazy var accessoryView: UIView = { |
| 32 | + let _accessory = ICKeyboardDismissAccessoryView() |
| 33 | + _accessory.dismissButton.addTarget(self, action: Selector("dismiss:"), forControlEvents: .TouchUpInside) |
| 34 | + return _accessory |
| 35 | + }() |
| 36 | + |
| 37 | + // MARK: - Initialization |
| 38 | + |
| 39 | + override public init(frame: CGRect) { |
| 40 | + super.init(frame: frame) |
| 41 | + inputAccessoryView = accessoryView |
| 42 | + } |
| 43 | + |
| 44 | + required public init?(coder aDecoder: NSCoder) { |
| 45 | + super.init(coder: aDecoder) |
| 46 | + inputAccessoryView = accessoryView |
| 47 | + } |
| 48 | + |
| 49 | + // MARK: - UIResponder |
| 50 | + |
| 51 | + override public func becomeFirstResponder() -> Bool { |
| 52 | + accessoryView.alpha = 1 |
| 53 | + return super.becomeFirstResponder() |
| 54 | + } |
| 55 | + |
| 56 | + // MARK: - Private Methods |
| 57 | + |
| 58 | + @IBAction private func dismiss(sender: UIButton) { |
| 59 | + resignFirstResponder() |
| 60 | + UIView.animateWithDuration(0.3) { |
| 61 | + self.accessoryView.alpha = 0 |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments