swift - 如何更改CAShapeLayer内的区域?

标签 swift core-graphics uibezierpath cashapelayer

我使用自定义 UIBezierPath 和填充颜色创建了 CAShapeLayer。如何更改路径内的区域?使其变大/变小。

    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

最佳答案

您无法更改 CAShapeLayer。

但是我通过两步就达到了这个效果。

首先 - 使用 bezierPath 选择一个区域,保存路径。

第二 - 创建 CGContext,使用 bezierPath 填充颜色的颜色在图像上添加图像,然后使用我们的路径添加蒙版并添加删除/取消删除图像功能。

关于swift - 如何更改CAShapeLayer内的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56970355/

相关文章:

ios - 通过降低速度逐渐减慢指尖陀螺的速度

ios - swift 从 Appdelegate 转至 UITabBarController

objective-c - 如何直接更新像素 - 使用 CGImage 和直接 CGDataProvider

iphone - CIFilter 无法正常工作,返回空图像

ios - 将 CIImage 转换为 CGImage 太慢

ios - 从 CGContextRef 获取 UIBezierPath 或 CGPath

iOS 屏幕绘图最佳实践

swift - Revenue cat 用户没有资格执行该操作。 & 用户不符合促销优惠资格

ios - KeyboardWillShowNotification、iOS8 和安全文本输入

ios - 在单个 CAShapeLayer 中对多个 UIBezier 路径进行动画处理