|
| 1 | +// |
| 2 | +// ICKeyboardDismissAccessoryView.swift |
| 3 | +// iCook |
| 4 | +// |
| 5 | +// Created by Ben on 27/08/2015. |
| 6 | +// Copyright (c) 2015 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 | +class ICKeyboardDismissAccessoryView: UIView { |
| 30 | + |
| 31 | + private(set) lazy var dismissButton: UIButton = { |
| 32 | + let _button = UIButton() |
| 33 | + _button.setImage(UIImage(named: "icook-iphone-button-hidekeyboard"), forState: .Normal) |
| 34 | + _button.backgroundColor = Constants.ButtonColor |
| 35 | + _button.exclusiveTouch = true |
| 36 | + _button.layer.cornerRadius = 4 |
| 37 | + _button.layer.shouldRasterize = true |
| 38 | + _button.layer.rasterizationScale = UIScreen.mainScreen().scale |
| 39 | + return _button |
| 40 | + }() |
| 41 | + |
| 42 | + // MARK: - Initialization |
| 43 | + |
| 44 | + override init(frame: CGRect) { |
| 45 | + super.init(frame: frame) |
| 46 | + setUpSubviews() |
| 47 | + } |
| 48 | + |
| 49 | + required init?(coder aDecoder: NSCoder) { |
| 50 | + super.init(coder: aDecoder) |
| 51 | + setUpSubviews() |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - UIView |
| 55 | + |
| 56 | + override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { |
| 57 | + for subview in subviews { |
| 58 | + if !subview.hidden && subview.alpha > 0 && |
| 59 | + subview.userInteractionEnabled && |
| 60 | + subview.pointInside(convertPoint(point, toView: subview), withEvent: event) { |
| 61 | + return true |
| 62 | + } |
| 63 | + } |
| 64 | + return false |
| 65 | + } |
| 66 | + |
| 67 | + // MARK: - Private Methods |
| 68 | + |
| 69 | + private struct Constants { |
| 70 | + static let ButtonColor = UIColor(red:0.21, green:0.2, blue:0.19, alpha:0.5) |
| 71 | + static let EdgePadding = CGFloat(7) |
| 72 | + static let InteractiveSize = CGSize(width: 44, height: 44) |
| 73 | + } |
| 74 | + |
| 75 | + private func setUpSubviews() { |
| 76 | + backgroundColor = UIColor.clearColor() |
| 77 | + |
| 78 | + addSubview(dismissButton) |
| 79 | + dismissButton.translatesAutoresizingMaskIntoConstraints = false |
| 80 | + |
| 81 | + let views = ["button": dismissButton] |
| 82 | + let metrics = [ |
| 83 | + "width": Constants.InteractiveSize.width, |
| 84 | + "height": Constants.InteractiveSize.height, |
| 85 | + "padding": Constants.EdgePadding |
| 86 | + ] |
| 87 | + |
| 88 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( |
| 89 | + "H:[button(width)]-(padding)-|", |
| 90 | + options: [], |
| 91 | + metrics: metrics, |
| 92 | + views: views |
| 93 | + )) |
| 94 | + addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( |
| 95 | + "V:[button(height)]-(padding)-|", |
| 96 | + options: [], |
| 97 | + metrics: metrics, |
| 98 | + views: views |
| 99 | + )) |
| 100 | + } |
| 101 | + |
| 102 | + // MARK: - Public Methods |
| 103 | + |
| 104 | + class func requiredHeight() -> CGFloat { |
| 105 | + return Constants.InteractiveSize.height + Constants.EdgePadding * 2 |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments