嘿,我是新编程的,我想知道是否有可能创建一个渐变效果,有2种颜色(例如,红色和蓝色)类型的UInt32。有什么建议吗?谢谢
对问题的补充
我的想法是我只有像素信息:
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
}但我不知道如何顺利地改变颜色,做出渐变效果,或以某种方式确定周长,并设置一层,不知道,任何想法都将是黄金。
发布于 2017-01-25 11:15:37
是的,可以使用CoreGraphics或CALayer。
为了简化,您可以覆盖返回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假设有两个数组作为属性,一个是locations,另一个是要添加到梯度中的colors。
locations代表:
每种颜色的起始位置。同样重要的是,这些数字应该在0.0到1.0之间。并与颜色数组配对。
https://stackoverflow.com/questions/41849669
复制相似问题