// // SearchAndDisplayAccessory.swift // CapCollector // // Created by Christoph on 09.10.19. // Copyright © 2019 CH. All rights reserved. // import UIKit class PassthroughView: UIView { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let view = super.hitTest(point, with: event) return view == self ? nil : view } } protocol CapAccessoryDelegate: class { func capSearchWasDismissed() func capSearch(didChange text: String) func capAccessoryDidDiscardImage() func capAccessory(shouldSave image: UIImage) func capAccessoryCameraButtonPressed() } class SearchAndDisplayAccessory: PassthroughView { // MARK: - Outlets @IBOutlet weak var newImageView: PassthroughView! @IBOutlet weak var capImage: RoundedImageView! @IBOutlet weak var saveButton: UIButton! @IBOutlet weak var deleteButton: UIButton! @IBOutlet weak var cameraButton: UIButton! @IBOutlet weak var searchBar: UISearchBar! // MARK: - Actions @IBAction func cameraButtonPressed() { delegate?.capAccessoryCameraButtonPressed() } @IBAction func saveButtonPressed() { if let image = capImage.image { delegate?.capAccessory(shouldSave: image) } } @IBAction func cancelButtonPressed() { discardImage() } // MARK: - Variables var view: UIView? weak var blurView: UIVisualEffectView? weak var currentBlurContraint: NSLayoutConstraint? weak var delegate: CapAccessoryDelegate? var currentImage: UIImage? { capImage.image } // MARK: - Setup convenience init(width: CGFloat) { let frame = CGRect(origin: .zero, size: CGSize(width: width, height: 145)) self.init(frame: frame) } override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { super.init(coder: coder) setup() } private func setup() { view = fromNib() view!.frame = bounds //view!.autoresizingMask = .flexibleHeight addSubview(view!) let blur = UIBlurEffect(style: .systemThinMaterial) let blurView = UIVisualEffectView(effect: blur) self.blurView = blurView blurView.translatesAutoresizingMaskIntoConstraints = false blurView.isUserInteractionEnabled = false insertSubview(blurView, at: 0) let t = searchBar.topAnchor.constraint(equalTo: blurView.topAnchor) let b = searchBar.bottomAnchor.constraint(equalTo: blurView.bottomAnchor) let l = leadingAnchor.constraint(equalTo: blurView.leadingAnchor) let r = trailingAnchor.constraint(equalTo: blurView.trailingAnchor) addConstraints([t, b, l, r]) currentBlurContraint = t self.newImageView.alpha = 0 self.newImageView.isHidden = true searchBar.text = nil searchBar.setShowsCancelButton(false, animated: false) searchBar.delegate = self cameraButton.setImage(UIImage.templateImage(named: "camera_square"), for: .normal) } // MARK: Search bar func dismissAndClearSearchBar() { searchBar.resignFirstResponder() searchBar.text = nil } // MARK: Cap image func showImageView(with image: UIImage) { capImage.image = image showImageView() } func discardImage() { dismissAndClearSearchBar() hideImageView() delegate?.capAccessoryDidDiscardImage() } func hideImageView() { currentBlurContraint?.isActive = false let t = searchBar.topAnchor.constraint(equalTo: blurView!.topAnchor) addConstraint(t) currentBlurContraint = t self.newImageView.alpha = 0 self.newImageView.isHidden = true self.capImage.image = nil } private func showImageView() { currentBlurContraint?.isActive = false let t = blurView!.topAnchor.constraint(equalTo: saveButton.topAnchor, constant: -8) addConstraint(t) currentBlurContraint = t self.newImageView.isHidden = false self.newImageView.alpha = 1 } } // MARK: - UISearchBarDelegate extension SearchAndDisplayAccessory: UISearchBarDelegate { func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() searchBar.text = nil delegate?.capSearchWasDismissed() } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { delegate?.capSearch(didChange: searchText) } }