使用渐变颜色SWIFT填充未定义的表单

时间:2017-01-24 15:51:17

标签: ios swift core-graphics gradient

我是编程新手,我不知道如何用渐变色填充未定义的几何形状...... 我设法做了一个简单的颜色:

 func fillRegion(pixelX: Int, pixelY: Int, withColor color: UIColor) {

    var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
    color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

    var newColor = (UInt32)(alpha*255)<<24 | (UInt32)(red*255)<<16 | (UInt32)(green*255)<<8 | (UInt32)(blue*255)<<0

    let pixelColor = regionsData.advanced(by: (pixelY * imageHeight) + pixelX).pointee

    if pixelColor == blackColor { return }

    var pointerRegionsData: UnsafeMutablePointer<UInt32> = regionsData
    var pointerImageData: UnsafeMutablePointer<UInt32> = imageData

    var pixelsChanged = false

    for i in 0...(imageHeight * imageHeight - 1) {
        if pointerRegionsData.pointee == pixelColor {
            pointerImageData = imageData.advanced(by: i)
            if pointerImageData.pointee != newColor {
//                    newColor = newColor + 1
                pointerImageData.pointee = newColor
                pixelsChanged = true
            }
        }
        pointerRegionsData = pointerRegionsData.successor()
    }

    if pixelsChanged {
        self.image = UIImage(cgImage: imageContext.makeImage()!)
        DispatchQueue.main.async {
            CATransaction.setDisableActions(true)
            self.layer.contents = self.image.cgImage
            self.onImageDraw?(self.image)
        }
        self.playTapSound()
    }
}

像素逐像素地填充颜色(忽略黑色)任何想法如何使用渐变颜色?谢谢!

2 个答案:

答案 0 :(得分:1)

您可以制作渐变图层并应用图像或形状图层作为其蒙版。这是一个游乐场。

    import PlaygroundSupport
    import UIKit

    class V: UIView {
        private lazy var gradientLayer: CAGradientLayer = {
            let gradientLayer = CAGradientLayer()
            gradientLayer.colors =  [UIColor.red.cgColor,
                                     UIColor.purple.cgColor,
                                     UIColor.blue.cgColor,
                                     UIColor.white.cgColor]
            gradientLayer.locations = [0, 0.3, 0.9, 1]
            gradientLayer.startPoint = CGPoint(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint(x: 0, y: 1)
            gradientLayer.mask = self.strokeLayer
            self.layer.addSublayer(gradientLayer)
            return gradientLayer
        }()

        private lazy var strokeLayer: CAShapeLayer = {
            let strokeLayer = CAShapeLayer()
            strokeLayer.path = UIBezierPath(ovalIn: CGRect(x:0, y: 0, width: 100, height: 100)).cgPath
            return strokeLayer
        }()

        override func layoutSubviews() {
            super.layoutSubviews()
            strokeLayer.path = UIBezierPath(ovalIn: bounds).cgPath
            gradientLayer.frame = bounds

            layer.addSublayer(gradientLayer)
        }
    }
    let v = V(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    PlaygroundPage.current.liveView = v

答案 1 :(得分:0)

我不是100%确定我理解这个问题,但似乎就像你想要用渐变填充任何旧形状一样,对吧?如果是这样,有几种方法可以做到这一点,但最简单的方法是制作一个与形状边界大小相同的渐变,然后将其作为颜色应用。我在我的电脑上输入这个,所以我确定会出现语法错误,但这里有......

let size = CGSize(width, height)
UIGraphicsRenderer(size, false, 0) // I KNOW I have this one wrong
let colors = [tColour.cgColor, bColour.cgColor] as CFArray
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradient(colorsSpace: colorSpace, colors: colors , locations: nil)

根据需要设置颜色数组,然后将其发送到UIImage。您可以使用位置:更改方向。