【问题标题】:Gradient effect with UInt32 Colors?UInt32 颜色的渐变效果?
【发布时间】:2017-01-25 10:51:40
【问题描述】:

嘿,我是编程新手,我想知道是否可以创建具有 UInt32 类型的 2 种颜色(例如红色和蓝色)的渐变效果。 有什么建议么?谢谢

问题补充

这个想法是我只有像素信息:

        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)

    let 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 {
                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()
    }
}

现在它填充了简单的颜色,但我需要以某种方式设置渐变,我没有合适的形式,我无法设置 CAShapeLayer...因为我只有像素数信息和他的颜色,虽然我可以在这里改变颜色:

if pointerImageData.pointee != newColor {
                pointerImageData.pointee = newColor
                pixelsChanged = true
            }

但我不知道如何进行平滑的颜色变化,制作渐变效果,或者以某种方式识别周边并设置图层,不知道,任何想法都会像黄金一样。

【问题讨论】:

  • “UInt32 颜色”是什么意思?
  • @Larme 颜色类型:var newColor = (UInt32)(alpha*255)
  • 要做一个渐变,你需要一个CGColor数组,如果我没记错的话,所以你需要把你的colorClass转换成UIColor,然后再转换成CGColor。
  • @Larme 我需要制作渐变效果,因为我不能使用渐变图层...我别无选择...
  • 要制作渐变效果,您需要什么样的对象/参数?因为iOS系统通常使用UIColor或者CGColor,所以不清楚。一切都是为了转换成您想要的。

标签: ios swift core-graphics gradient uint32


【解决方案1】:

是的,可以使用CoreGraphicsCALayer
为简化起见,您可以覆盖返回 CAGradientLayer 的视图层。
这是一个实现:

@implementation PGSGradientView

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initializeGradient];
    }
    return self;
}

- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeGradient];
    }
    return self;
}

- (void) initializeGradient {
    [(CAGradientLayer*)self.layer setLocations:locations];
    [(CAGradientLayer*)self.layer setColors:colors];

}

@end

假设您有 2 个数组作为属性,一个是 locations,另一个是您要添加到渐变中的 colors
locations 表示:

每种颜色的起始位置。还有,和 这很重要,这些数字应该强制范围从 0.0 到 1.0。 并与颜色数组配对。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多