Swiftで図形を拡大縮小した時にレイヤーの再描画をしたい。
swiftでMSOfficeの図形のハンドルのようなものが出来ないかと試行錯誤しております。以下のコードでハンドルとなるovalを移動するとviewに描かれている丸が拡大縮小されるようにしたいのですが、一度viewに描かれた丸がviewをremoveFromSuperlayerで消しても残ってしまいます。viewに描画するクラスをUIViewにしたりCALayerにしたりして試したのですが丸が消えてくれません。どうにか良い方法をご教授頂けないでしょうか?
import UIKit
class ViewController: UIViewController {
var selectLayer:CALayer!
var touchLastPoint:CGPoint!
var btn01:UIButton! = UIButton()
var btn02:UIButton! = UIButton()
var oval_w:CGFloat! = 30
var px01:CGFloat! = 50,py01:CGFloat! = 150
var px02:CGFloat! = 150,py02:CGFloat! = 150
var view01:MyView! = MyView()
var oval01:MyLayer! = MyLayer()
var oval02:MyLayer! = MyLayer()
override func viewDidLoad() {
super.viewDidLoad()
btn01.frame = CGRect(x: 50, y: 50, width: 80, height: 30)
btn01.backgroundColor = UIColor.blue
btn01.addTarget(self, action: #selector(btn01Tapped), for: .touchUpInside)
self.view.addSubview(btn01)
btn02.frame = CGRect(x: 140, y: 50, width: 80, height: 30)
btn02.backgroundColor = UIColor.blue
btn02.addTarget(self, action: #selector(btn02Tapped), for: .touchUpInside)
self.view.addSubview(btn02)
}
@objc func btn01Tapped(){
view01.frame = CGRect(x: 50, y: 200, width: 100, height: 100)
view01.layer.borderColor = UIColor.black.cgColor
view01.layer.borderWidth = 1
view01.viewOval()
self.view.addSubview(view01)
oval01.frame = CGRect(x: px01, y: py01, width: oval_w, height: oval_w)
oval01.drawOval()
self.view.layer.addSublayer(oval01)
oval02.frame = CGRect(x: px02, y: py02, width: oval_w, height: oval_w)
oval02.drawOval()
self.view.layer.addSublayer(oval02)
}
@objc func btn02Tapped(){
view01.removeFromSuperview()
oval01.removeFromSuperlayer()
}
func hitLayer(touch:UITouch) -> CALayer{
var touchPoint = touch.location(in: self.view)
touchPoint = self.view.layer.convert(touchPoint, to: self.view.layer.superlayer)
return self.view.layer.hitTest(touchPoint)!
}
func selectLayerFunc(layer:CALayer?){
if(layer == self.view.layer) || (layer == nil){
selectLayer = nil
return
}
selectLayer = layer
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
selectLayer = nil
let touch:UITouch = touches.first!
let layer:CALayer = hitLayer(touch: touch)
let touchPoint:CGPoint = touch.location(in: self.view)
touchLastPoint = touchPoint
self.selectLayerFunc(layer: layer)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first!
let touchPoint:CGPoint = touch.location(in: self.view)
let touchOffsetPoint:CGPoint = CGPoint(x: touchPoint.x - touchLastPoint.x, y: touchPoint.y - touchLastPoint.y)
touchLastPoint = touchPoint
if(selectLayer != nil){
let px:CGFloat = selectLayer.frame.origin.x
let py:CGFloat = selectLayer.frame.origin.y
CATransaction.begin()
CATransaction.setDisableActions(true)
selectLayer.frame.origin.x = px + touchOffsetPoint.x
selectLayer.frame.origin.y = py + touchOffsetPoint.y
view01.frame.size.width = oval02.frame.origin.x - oval01.frame.origin.x
CATransaction.commit()
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
view01.viewOval()
}
}
import UIKit
class MyView: UIView {
func viewOval(){
let oval = CAShapeLayer()
oval.strokeColor = UIColor.red.cgColor
oval.fillColor = UIColor.clear.cgColor
oval.lineWidth = 1
oval.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)).cgPath
self.layer.addSublayer(oval)
}
}