缩放 CAShapeLayer 时的意外行为
Unexpected behavior in scaling CAShapeLayer
我正在使用以下代码来缩放 CAShapelayer 实例(即 self.view.layer.sublayers![0]):
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
self.view = Map(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
let pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(self.didPinch(_:)))
self.view.addGestureRecognizer(pinchGR)
}
@objc func didPinch(_ pinchGR: UIPinchGestureRecognizer)
{
let transformation = CGAffineTransform(a: pinchGR.scale, b: 0, c: 0, d: pinchGR.scale, tx: 0, ty: 0)
if pinchGR.state == .began || pinchGR.state == .changed
{
self.view.layer.sublayers![0].setAffineTransform(transformation)
}
}}
class Map: UIView{
override init(frame: CGRect)
{
super.init(frame: frame)
self.backgroundColor = UIColor.blue
drawTestShape()
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
func drawTestShape()
{
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.frame
let path = UIBezierPath()
shapeLayer.lineWidth = 5.0
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.backgroundColor = UIColor.red.cgColor
path.move(to: CGPoint(x:200,y:200))
path.addLine(to: CGPoint(x:300,y:300))
path.addLine(to: CGPoint(x:100,y:300))
path.close()
shapeLayer.path = path.cgPath
self.layer.addSublayer(shapeLayer)
}}
缩放在第一个捏合手势中正确执行。但是,当应用第二个捏合手势时,CAShapelayer 实例再次从其初始大小开始,而不是从第一次手势后的大小开始。
我真的无法理解缩放图层的想法,但如果你必须这样做,请尝试更多类似的东西;这是实现捏合手势识别器的标准模式,因此最好坚持使用它:
@IBAction func didPinch(_ pinchGR: UIPinchGestureRecognizer) {
self.view.layer.sublayers![0].setAffineTransform(
self.view.layer.sublayers![0].affineTransform().scaledBy(
x:pinchGR.scale, y:pinchGR.scale))
pinchGR.scale = 1.0;
}
我正在使用以下代码来缩放 CAShapelayer 实例(即 self.view.layer.sublayers![0]):
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
self.view = Map(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
let pinchGR = UIPinchGestureRecognizer(target: self, action: #selector(self.didPinch(_:)))
self.view.addGestureRecognizer(pinchGR)
}
@objc func didPinch(_ pinchGR: UIPinchGestureRecognizer)
{
let transformation = CGAffineTransform(a: pinchGR.scale, b: 0, c: 0, d: pinchGR.scale, tx: 0, ty: 0)
if pinchGR.state == .began || pinchGR.state == .changed
{
self.view.layer.sublayers![0].setAffineTransform(transformation)
}
}}
class Map: UIView{
override init(frame: CGRect)
{
super.init(frame: frame)
self.backgroundColor = UIColor.blue
drawTestShape()
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
func drawTestShape()
{
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.frame
let path = UIBezierPath()
shapeLayer.lineWidth = 5.0
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.backgroundColor = UIColor.red.cgColor
path.move(to: CGPoint(x:200,y:200))
path.addLine(to: CGPoint(x:300,y:300))
path.addLine(to: CGPoint(x:100,y:300))
path.close()
shapeLayer.path = path.cgPath
self.layer.addSublayer(shapeLayer)
}}
缩放在第一个捏合手势中正确执行。但是,当应用第二个捏合手势时,CAShapelayer 实例再次从其初始大小开始,而不是从第一次手势后的大小开始。
我真的无法理解缩放图层的想法,但如果你必须这样做,请尝试更多类似的东西;这是实现捏合手势识别器的标准模式,因此最好坚持使用它:
@IBAction func didPinch(_ pinchGR: UIPinchGestureRecognizer) {
self.view.layer.sublayers![0].setAffineTransform(
self.view.layer.sublayers![0].affineTransform().scaledBy(
x:pinchGR.scale, y:pinchGR.scale))
pinchGR.scale = 1.0;
}