swiftでMSOfficeの図形のハンドルのようなものが出来ないかと試行錯誤しております。以下のコードでハンドルとなるovalを移動するとviewに描かれている丸が拡大縮小されるようにしたいのですが、viewをremoveFromSuperlayerで消してviewを再表示しても消す前に表示されていた丸が残ってしまい、再表示するたびに丸が増えていってしまいます。viewに描画するクラスをUIViewにしたりCALayerにしたりして試したのですが丸が消えてくれません。どうにか良い方法をご教授頂けないでしょうか?
Swift4
1import UIKit 2class ViewController: UIViewController { 3 var selectLayer:CALayer! 4 var touchLastPoint:CGPoint! 5 var btn01:UIButton! = UIButton() 6 var btn02:UIButton! = UIButton() 7 8 var oval_w:CGFloat! = 30 9 var px01:CGFloat! = 50,py01:CGFloat! = 150 10 var px02:CGFloat! = 150,py02:CGFloat! = 150 11 12 var view01:MyView! = MyView() 13 var oval01:MyLayer! = MyLayer() 14 var oval02:MyLayer! = MyLayer() 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 btn01.frame = CGRect(x: 50, y: 50, width: 80, height: 30) 20 btn01.backgroundColor = UIColor.blue 21 btn01.addTarget(self, action: #selector(btn01Tapped), for: .touchUpInside) 22 self.view.addSubview(btn01) 23 24 btn02.frame = CGRect(x: 140, y: 50, width: 80, height: 30) 25 btn02.backgroundColor = UIColor.blue 26 btn02.addTarget(self, action: #selector(btn02Tapped), for: .touchUpInside) 27 self.view.addSubview(btn02) 28 } 29 @objc func btn01Tapped(){ 30 view01.frame = CGRect(x: 50, y: 200, width: 100, height: 100) 31 view01.layer.borderColor = UIColor.black.cgColor 32 view01.layer.borderWidth = 1 33 view01.viewOval() 34 self.view.addSubview(view01) 35 36 oval01.frame = CGRect(x: px01, y: py01, width: oval_w, height: oval_w) 37 oval01.drawOval() 38 self.view.layer.addSublayer(oval01) 39 oval02.frame = CGRect(x: px02, y: py02, width: oval_w, height: oval_w) 40 oval02.drawOval() 41 self.view.layer.addSublayer(oval02) 42 } 43 @objc func btn02Tapped(){ 44 view01.removeFromSuperview() 45 oval01.removeFromSuperlayer() 46 } 47 func hitLayer(touch:UITouch) -> CALayer{ 48 var touchPoint = touch.location(in: self.view) 49 touchPoint = self.view.layer.convert(touchPoint, to: self.view.layer.superlayer) 50 return self.view.layer.hitTest(touchPoint)! 51 } 52 func selectLayerFunc(layer:CALayer?){ 53 if(layer == self.view.layer) || (layer == nil){ 54 selectLayer = nil 55 return 56 } 57 selectLayer = layer 58 } 59 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 60 selectLayer = nil 61 let touch:UITouch = touches.first! 62 let layer:CALayer = hitLayer(touch: touch) 63 let touchPoint:CGPoint = touch.location(in: self.view) 64 touchLastPoint = touchPoint 65 self.selectLayerFunc(layer: layer) 66 } 67 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 68 let touch:UITouch = touches.first! 69 let touchPoint:CGPoint = touch.location(in: self.view) 70 let touchOffsetPoint:CGPoint = CGPoint(x: touchPoint.x - touchLastPoint.x, y: touchPoint.y - touchLastPoint.y) 71 touchLastPoint = touchPoint 72 if(selectLayer != nil){ 73 let px:CGFloat = selectLayer.frame.origin.x 74 let py:CGFloat = selectLayer.frame.origin.y 75 76 CATransaction.begin() 77 CATransaction.setDisableActions(true) 78 selectLayer.frame.origin.x = px + touchOffsetPoint.x 79 selectLayer.frame.origin.y = py + touchOffsetPoint.y 80 view01.frame.size.width = oval02.frame.origin.x - oval01.frame.origin.x 81 82 CATransaction.commit() 83 } 84 } 85 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 86 view01.viewOval() 87 } 88} 89 90import UIKit 91 92class MyLayer: CALayer { 93 func drawOval(){ 94 let ovalLayer = CAShapeLayer() 95 ovalLayer.strokeColor = UIColor.blue.cgColor 96 ovalLayer.fillColor = UIColor.clear.cgColor 97 ovalLayer.lineWidth = 1 98 ovalLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)).cgPath 99 self.addSublayer(ovalLayer) 100 } 101 102} 103
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/09 00:56