Board.swift 2.58 KB
import Foundation
import UIKit
import GeneralUtils

@IBDesignable class Board: UIView {
    @IBOutlet weak var vRoot: UIView!
    @IBOutlet weak var tvDate: UILabel!
    @IBOutlet weak var tvNote: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    func xibSetup() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "Board", bundle: bundle)
        if let view = nib.instantiate(withOwner: self, options: nil)[0] as? UIView {
            view.frame = bounds
            view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
            addSubview(view)
        }
    }

    func initData() {
        let date = Date()
        setDate(date: date)
    }

    private func setDate(date: Date) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMM d, yyyy"
        dateFormatter.locale = Locale(identifier: "ja_JP")
        tvDate.text = dateFormatter.string(from: date)
    }

    @IBAction func noteClick(_ sender: Any) {
        let dialog = DialogUtils.builderDialog(showCloseButton: false, showTitle: false)
        let edt = dialog.addTextField()
        dialog.addButton(LocalizedString("OK"), action: {
            self.tvNote.text = " " + (edt.text ?? "")
        })
        dialog.showTitle("", subTitle: "備考", style: .edit)
        edt.becomeFirstResponder()
    }

    @IBAction func dateClick(_ sender: Any) {
        DatePickerDialog().show("撮影日", datePickerMode: .date, callback: { date in
                    if let date = date {
                        self.setDate(date: date)
                        //TODO
                    }
                })
    }

    func scale() {
        delayExcute(1, block: {
            print("Scale")
            UIView.animate(withDuration: 5, animations: {
                self.vRoot.transform = CGAffineTransform.identity.scaledBy(x: 0.3, y: 0.3)
                self.vRoot.frame.origin.x = 0
                self.vRoot.frame.origin.y = 0
            })
        })
    }

    func delayExcute(_ delay: TimeInterval, queueParam: DispatchQueue? = nil, block: @escaping () -> ()) {
        var queue: DispatchQueue!
        if queueParam == nil {
            queue = DispatchQueue.main
        } else {
            queue = queueParam
        }
        let time = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
        queue.asyncAfter(deadline: time, execute: block)
    }
}