結論から言うと
iPad(Applepencil)のメモアプリを作成しています。
そのアプリでは文字(絵)を描くことはもちろん、Applepencil(iPadに対して)の角度や力量を数値化し、その数値をファイルに保存することを目的としていますが、そのやり方が分かりません。
現在、文字を描くところまではできております。ただし、角度や力量を数値化しファイルに保存するところはできておりません。角度や力量に関してはViewでは表示できますが、文字を描くのと数値を表示することのどちらかしか出来ません。同時に動作をすることが出来てません。
本来はその数値をViewではなくコンソールに表示できたら完璧ですが...そのやり方が調べても分からない状況にあります。
どうか分かる方がいれば教えていただきたいです。よろしく御願いします。
DrawView(絵を描くコード)
import UIKit class DrawView: UIView { var currentDrawing: Drawing? var finishedDrawings = [Drawing]() var currentColor = UIColor.black override func draw(_ rect: CGRect) { for drawing in finishedDrawings { drawing.color.setStroke() stroke(drawing: drawing) } if let drawing = currentDrawing { drawing.color.setStroke() stroke(drawing: drawing) } } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first! let location = touch.location(in: self) currentDrawing = Drawing() currentDrawing?.color = currentColor currentDrawing?.points.append(location) setNeedsDisplay() } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first! let location = touch.location(in: self) currentDrawing?.points.append(location) setNeedsDisplay() } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if var drawing = currentDrawing { let touch = touches.first! let location = touch.location(in: self) drawing.points.append(location) finishedDrawings.append(drawing) } currentDrawing = nil setNeedsDisplay() } func clear() { finishedDrawings.removeAll() setNeedsDisplay() } func undo() { if finishedDrawings.count == 0 { return } finishedDrawings.remove(at: finishedDrawings.count - 1) setNeedsDisplay() } func setDrawingColor(color : UIColor){ currentColor = color } func stroke(drawing: Drawing) { let path = UIBezierPath() path.lineWidth = 10.0 path.lineCapStyle = .round path.lineJoinStyle = .round let begin = drawing.points[0]; path.move(to: begin) if drawing.points.count > 1 { for i in 1...(drawing.points.count - 1) { let end = drawing.points[i] path.addLine(to: end) } } path.stroke() } }
AddViewController(数値を表示し、userdefaultsで保存している)
import UIKit class AddViewController: UIViewController { @IBOutlet weak var drawView: DrawView! @IBOutlet weak var segmentedControl: UISegmentedControl! @IBOutlet var altitudeAngleLabel:UILabel! @IBOutlet var azimuthAngleLabel:UILabel! @IBOutlet var forceLabel:UILabel! override func viewDidLoad() { super.viewDidLoad() segmentedControl.selectedSegmentIndex = 0 resetLabel() } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { exploreTouchState(touch: touch) } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { exploreTouchState(touch: touch) } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { resetLabel() } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { resetLabel() } private func exploreTouchState(touch: UITouch) { altitudeAngleLabel.text = String(format: "%.2f", touch.altitudeAngle) print(altitudeAngleLabel.text as Any) azimuthAngleLabel.text = String(format: "%.2f", touch.azimuthAngle(in: self.view)) forceLabel.text = String(format: "%.2f", touch.force) UserDefaults.standard.set(altitudeAngleLabel, forKey: "alti") UserDefaults.standard.set(azimuthAngleLabel, forKey: "azimu") UserDefaults.standard.set(forceLabel, forKey: "force") UserDefaults.standard.object(forKey: "alti") UserDefaults.standard.object(forKey: "azimu") UserDefaults.standard.object(forKey: "force") } private func resetLabel() { altitudeAngleLabel?.text = "" azimuthAngleLabel?.text = "" forceLabel?.text = "" } @IBAction func clearTapped(_ sender: Any) { drawView.clear() } @IBAction func undoTapped(_ sender: Any) { drawView.undo() } @IBAction func colorChanged(_ sender: Any) { var c = UIColor.black switch segmentedControl.selectedSegmentIndex { case 1: c = UIColor.blue break case 2: c = UIColor.red break default: break } drawView.setDrawingColor(color: c) } }
どうか助けてください!よろしく御願いします。
「コンソールに表示」とは print のことでしょうか。そして、データの保存先が UserDefaults なのはそろそろ無理後あるのでは…。
ありがとうございます!
printのことです。
SQLを使わないと無理ということでしょうか?
返事いただけると嬉しいです!
よく見たら DrawView と AddViewController の両方でイベント処理をしているようですね。(最近生のイベント処理してないのでよく分かりませんが、それで動きましたっけ? Apple Pencil 持ってないから試せないし…。)
あと、UserDefaults への保存に関しては、最新の情報を 1 セットだけしか保存してないように見えますが、そもそも何のために角度や力量を保存したいのでしょうか?
ありがとうございます!両方でイベントはできないものでしょうか?
userdefaultsをfor文で回さないといけないですね!ありがとうございます。
保存する目的としては卒業研究で使用するためです。
> 両方でイベントはできないものでしょうか?
できるかどうかは分かりませんが、たとえできたとしても、一箇所でまとめてイベントを受け取った方が良いような…。
何のために? と聞いたのはどういうデータが欲しいかを知りたかったんですが、聞き方が良くなかったですね。
たぶん時系列で角度や力量がどう変化するかのデータが欲しいんだと思いますが、UserDefaults を for 文で回すより、データをメモリ上に溜めておいて、最終的に CSV とかで書き出すとかの方が良いのでは…。
ありがとうございます!
おっしゃる通りですね!やってみます!
また何か聞きたいことがあれば質問するかもしれません。
その時はよろしく御願いします!
あなたの回答
tips
プレビュー