Как изменить область внутри CAShapeLayer? - PullRequest
2 голосов
/ 10 июля 2019

Я создал CAShapeLayer с пользовательским UIBezierPath и заливкой цвета.Как я могу изменить область внутри пути?Увеличьте / уменьшите.

    private var path = UIBezierPath()
    private var shapeLayer = CAShapeLayer()

    override public func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch? {
            let touchPoint = touch.location(in: self)
            path.move(to: touchPoint)
        }
    }

    override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch? {
            let touchPoint = touch.location(in: self)
            path.addLine(to: touchPoint)
            shapeLayer.path = path.cgPath
            shapeLayer.strokeColor = strokeColor.cgColor
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.lineWidth = lineWidth
            layer.addSublayer(shapeLayer)
        }
    }

    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch? {
            let touchPoint = touch.location(in: self)
            path.addLine(to: touchPoint)
            path.close()
            shapeLayer.path = path.cgPath
            shapeLayer.strokeColor = UIColor.clear.cgColor
            shapeLayer.fillColor = strokeColor.cgColor
            shapeLayer.lineWidth = lineWidth
            layer.addSublayer(shapeLayer)
        }
    }

Так что у меня проблема с изменением области.Как изменить область с закрашенным цветом?Или как изменить путь с / или без каких-либо точек?

Необходимые шаги: - выбрать кадр с помощью жестов;- Цвет заливки;- обновить рамку, чтобы сделать ее больше / меньше с помощью жестов (касания начинались / двигались / заканчивались)

Аналогично этой библиотеке https://github.com/TinyCrayon/TinyCrayon-iOS-SDK

1 Ответ

2 голосов
/ 17 июля 2019

Вы не можете изменить CAShapeLayer.

Но я достиг этого эффекта за 2 шага.

Первый - выберите область с помощью bezierPath, сохраните путь.

Второе - создайте CGContext, добавьте изображение поверх нашего изображения с цветом заливки bezierPath, затем добавьте маску с нашим путем и добавьте функцию стирания / удаления изображения.

...