iPhoneでタッチでお絵描きが出来るアプリを、swiftで作成中です。
滑らかな線を引くために、以下の処理をしています。
タッチ座標①
↓中間点①
タッチ座標②
↓中間点②
タッチ座標③
タッチ座標間の中間点を計算し、中間点①と中間点②を、タッチ座標②をコントロールポイントとした QuadBezierPath で結びます。
最初の指の移動では、最初にタッチした座標(A)と移動した後の座標(B)しかないので、最初にタッチした座標と(A)(B)の中間点を直線で結びます。
最後の指の移動についても同様で、最後の中間点と最終的に指を離した座標を直線で結びます。
###発生している問題・エラーメッセージ
エラーは出ていないのですが、最初のタッチで画面左上の座標(0,0)からタッチしたポイントまでいきなり線が引かれてしまいます。
また、指を離して、次にタッチすると、その2点間にも線が引かれてしまいます。
###該当のソースコード
swift3.0で書いてます。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView! var lastPoint = CGPoint.zero var currentPoint = CGPoint.zero var midPoint1 = CGPoint.zero var midPoint2 = CGPoint.zero var swiped = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } //直線の描画 func drawLines(fromPoint:CGPoint,toPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addLine(to:CGPoint(x:toPoint.x,y:toPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(1) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //曲線の描画 func drawCurves(fromPoint:CGPoint,toPoint:CGPoint,controlPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addQuadCurve(to:CGPoint(x:toPoint.x,y:toPoint.y),control:CGPoint(x:controlPoint.x,y:controlPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(5) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //タッチスタート override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ lastPoint = touch.location(in: self.view) } } //タッチ移動 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ //初回の移動なら最初の中間点まで直線を引く if swiped == false{ let currentPoint = touch.location(in: self.view) let midPoint1 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawLines(fromPoint:lastPoint,toPoint:midPoint1) lastPoint = currentPoint swiped = true }else{ //2回目以降の移動なら中間点を繋いで曲線を引く let currentPoint = touch.location(in: self.view) let midPoint2 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawCurves(fromPoint:midPoint1,toPoint:midPoint2,controlPoint:lastPoint) midPoint1 = midPoint2 lastPoint = currentPoint swiped = true } } } //最後のタッチは、最後の中間点から最終点まで直線を引く override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ let currentPoint = touch.location(in: self.view) drawLines(fromPoint: midPoint1, toPoint: currentPoint) swiped = false } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
なにぶん初心者なもので、質問の書き方すらよく分かっていないのですが、上記コードのどこに問題があるのか、教えて頂けると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。