Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UInt32颜色的渐变效果?_Ios_Swift_Core Graphics_Gradient_Uint32 - Fatal编程技术网

Ios UInt32颜色的渐变效果?

Ios UInt32颜色的渐变效果?,ios,swift,core-graphics,gradient,uint32,Ios,Swift,Core Graphics,Gradient,Uint32,嘿,我是编程新手,我想知道是否有可能创建一个UInt32类型的具有2种颜色(例如红色和蓝色)的渐变效果。 有什么建议吗?谢谢 问题的补充 我的想法是,我只有像素信息: func fillRegion(pixelX: Int, pixelY: Int, withColor color: UIColor) { var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0

嘿,我是编程新手,我想知道是否有可能创建一个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()
    }
}

但是我不知道如何平滑地改变颜色,产生渐变效果,或者以某种方式确定周长并建立一个图层,不知道,任何想法都会像金子一样。

是的,使用
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
假设您有两个数组作为属性,一个是
位置
,另一个是要添加到渐变中的
颜色。
位置
表示:

每种颜色的起始位置。而且 这很重要,这些数字必须在0.0到0.0之间 1.0. 并与颜色数组配对


“UInt32 Colors”是什么意思?@Larme color类型:var newColor=(UInt32)(alpha*255)要做渐变,你需要一个CGColor数组,如果我没记错的话,所以你需要将你的colorClass转换成UIColor,然后再转换成CGColor。@Larme我需要做渐变效果,因为我不能使用渐变层。。。我别无选择…要制作渐变效果,你需要什么样的对象/参数?因为iOS系统通常使用UIColor或CGColor,所以不清楚。这都是关于转换成你想要的。
@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