Menu in iOS

This commit is contained in:
Ivan Murashov
2018-05-20 13:44:15 +03:00
parent 23a3bf1a9c
commit d9400a3df9
34 changed files with 317 additions and 1218 deletions

View File

@@ -1,5 +1,7 @@
import UIKit
import Flutter
import DropDown
import ZXingObjC
@objc class ScannerViewController: UIViewController, ZXCaptureDelegate, UITextFieldDelegate {
@@ -28,28 +30,18 @@ import Flutter
}
}
// @IBOutlet weak var decodedLabel: UILabel!
private var captureSizeTransform: CGAffineTransform?
var captureSizeTransform: CGAffineTransform?
var buttonState: ButtonState = .card
var platformChannel: FlutterMethodChannel?
// Квадрат для наведения на цель (надеюсь)
let scanRectView = UIView()
//Вьюшка для верхнего меню
let topView = UIView()
//Окно ввода кода
let textField = UITextField()
//Кнопка настроек
var settingButton: UIButton!
var strings = [String:String]()
let scanRectView = UIView()
let header = UIView()
let textField = UITextField()
let settingsButton = UIButton(type: .system)
let searchType = UIButton(type: .system)
let dropDown = DropDown()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@@ -61,22 +53,22 @@ import Flutter
func getInputHint() -> String {
switch self.buttonState {
case .card: return strings["enter_manual"]!
case .phone: return strings["enter_phone"]!
case .card: return strings["enter_manual"]!
case .phone: return strings["enter_phone"]!
}
}
func getErrorText() -> String {
switch self.buttonState {
case .card: return strings["user_card_not_found"]!
case .phone: return strings["user_phone_not_found"]!
case .card: return strings["user_card_not_found"]!
case .phone: return strings["user_phone_not_found"]!
}
}
func setButtonState() {
switch self.buttonState {
case .card: self.buttonState = .phone
case .phone: self.buttonState = .card
case .card: self.buttonState = .phone
case .phone: self.buttonState = .card
}
}
@@ -84,57 +76,190 @@ import Flutter
override func viewDidLoad() {
super.viewDidLoad()
capture.camera = capture.back()
capture.focusMode = .continuousAutoFocus
view.layer.addSublayer((capture.layer)!)
view.addSubview(scanRectView)
view.addSubview(header)
view.bringSubview(toFront: header)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ScannerViewController.hideKeyboard)))
settingButton = UIButton(type: .system)
settingButton.addTarget(self, action: #selector(ScannerViewController.buttonTouch), for: .touchUpInside)
topView.addSubview(textField)
topView.addSubview(settingButton)
view.addSubview(topView)
view.bringSubview(toFront: topView)
initCamera()
initSearchTypeButton()
initTextFIeld()
initSettingsButton()
initDropDown()
initHeader()
}
private func initCamera() {
capture.camera = capture.back()
capture.focusMode = .continuousAutoFocus
}
private func initHeader() {
header.addSubview(textField)
header.addSubview(searchType)
header.addSubview(settingsButton)
}
private func initTextFIeld() {
textField.delegate = self
let tap = UITapGestureRecognizer(target: self, action: #selector(ScannerViewController.hideKeyboard))
view.addGestureRecognizer(tap)
settingButton.setImage(self.buttonState.icon, for: .normal)
textField.placeholder = self.getInputHint()
// textField.text = "79087654321"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
topView.backgroundColor = UIColor.white
textField.borderStyle = .roundedRect
capture.delegate = self
applyOrientation()
private func initSearchTypeButton() {
searchType.setImage(self.buttonState.icon, for: .normal)
searchType.addTarget(self, action: #selector(ScannerViewController.buttonTouch), for: .touchUpInside)
}
override func viewWillLayoutSubviews() {
scanRectView.frame = view.bounds
topView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 56)
settingButton.frame = CGRect(x: 8, y: 26, width: 20, height: 20)
textField.frame = CGRect(x: settingButton.frame.maxX + 8, y: 21,
width: view.frame.size.width - settingButton.frame.maxX - 16, height: 30)
private func initDropDown() {
dropDown.anchorView = settingsButton
dropDown.dataSource = [strings["settings"]!, strings["faq"]!]
dropDown.selectionAction = { (index: Int, item: String) in
if index == 0 {
self.platformChannel?.invokeMethod("settings", arguments: nil)
} else if index == 1 {
self.platformChannel?.invokeMethod("faq", arguments: nil)
}
self.dismiss(animated: false)
}
}
private func initSettingsButton() {
settingsButton.setImage(UIImage(named: "more")!, for: .normal)
settingsButton.addTarget(self, action: #selector(ScannerViewController.settingsTouch), for: .touchUpInside)
}
func hideKeyboard() {
view.endEditing(false)
}
func buttonTouch(){
func buttonTouch() {
setButtonState()
settingButton.setImage(self.buttonState.icon, for: .normal)
searchType.setImage(self.buttonState.icon, for: .normal)
textField.placeholder = self.getInputHint()
}
func settingsTouch() {
dropDown.show()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
header.backgroundColor = UIColor.white
textField.borderStyle = .roundedRect
capture.delegate = self
applyOrientation()
}
// TODO: Вынести эту копипасту в методы, когда будет время
override func viewWillLayoutSubviews() {
// TODO: Надо бы уйти от констант, переписать на отступы какие-нибудь
scanRectView.frame = view.bounds
header.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 56)
searchType.frame = CGRect(x: 8, y: 26, width: 20, height: 20)
textField.frame = CGRect(x: searchType.frame.maxX + 8, y: 21, width: view.frame.size.width - searchType.frame.maxX - 48, height: 30)
settingsButton.frame = CGRect(x: view.frame.size.width - 30, y: 26, width: 20, height: 20)
var path = UIBezierPath()
path.move(to: CGPoint(x: 32, y: view.frame.size.height / 2))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: view.frame.size.height / 2))
var shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 2
view.layer.addSublayer(shapeLayer)
path = UIBezierPath()
path.move(to: CGPoint(x: 32, y: (view.frame.size.height / 2) - 32))
path.addLine(to: CGPoint(x: 32, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: 64, y: (view.frame.size.height / 2) - 64))
path.move(to: CGPoint(x: view.frame.size.width - 64, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) - 32))
path.move(to: CGPoint(x: 32, y: (view.frame.size.height / 2) + 32))
path.addLine(to: CGPoint(x: 32, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: 64, y: (view.frame.size.height / 2) + 64))
path.move(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) + 32))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: view.frame.size.width - 64, y: (view.frame.size.height / 2) + 64))
shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 2
view.layer.addSublayer(shapeLayer)
path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 56))
path.addLine(to: CGPoint(x: view.frame.size.width, y: 56))
path.addLine(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: 0, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: 0, y: 56))
path.close()
shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
view.layer.addSublayer(shapeLayer)
path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: 32, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: 32, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: 0, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: 0, y: (view.frame.size.height / 2) - 64))
path.close()
shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
view.layer.addSublayer(shapeLayer)
path = UIBezierPath()
path.move(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) - 64))
path.addLine(to: CGPoint(x: view.frame.size.width - 32, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) - 64))
path.close()
shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
view.layer.addSublayer(shapeLayer)
path = UIBezierPath()
path.move(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: 0, y: (view.frame.size.height / 2) + 64))
path.addLine(to: CGPoint(x: 0, y: view.frame.size.height))
path.addLine(to: CGPoint(x: view.frame.size.width, y: view.frame.size.height))
path.addLine(to: CGPoint(x: view.frame.size.width, y: (view.frame.size.height / 2) + 64))
path.close()
shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
view.layer.addSublayer(shapeLayer)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("User from manual input: \(textField.text)")
print("User from manual input: \(textField.text!)")
sendResult(textField.text!)
return true
}