我想从渐变层的触摸点上色,怎么做?

Posted

技术标签:

【中文标题】我想从渐变层的触摸点上色,怎么做?【英文标题】:I want to color from touch point of gradient layer, how? 【发布时间】:2016-09-26 06:42:07 【问题描述】:

我想从渐变层的接触点上色。

我试过了:

func viewtap(sender: UITapGestureRecognizer) 
    let touchPoint = sender.locationInView(self.gradientview) // Change to whatever view you want the point for
    print("\(touchPoint))")
    colorOfPoint(touchPoint)

我使用了这个方法,但它只给出原始颜色。

func colorOfPoint(point:CGPoint) -> UIColor

    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()!
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)

    var pixelData:[UInt8] = [0, 0, 0, 0]

    let context = CGBitmapContextCreate(&pixelData, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)
    CGContextTranslateCTM(context, -point.x, -point.y);
    self.view.layer.renderInContext(context!)

    let red:CGFloat = CGFloat(pixelData[0])/CGFloat(255.0)
    let green:CGFloat = CGFloat(pixelData[1])/CGFloat(255.0)
    let blue:CGFloat = CGFloat(pixelData[2])/CGFloat(255.0)
    let alpha:CGFloat = CGFloat(pixelData[3])/CGFloat(255.0)

    let color:UIColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
    colorChange.tintColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
    return color    

渐变层:

【问题讨论】:

【参考方案1】:

总是在 uicolor 之后给出.CGColor。现在我将从UIView中的任何颜色中挑选颜色

    let gradientLayer = CAGradientLayer()

    gradientLayer.frame = self.shadowview.bounds

    let colorTop = UIColor(red:CGFloat(RedColor)/255.0 , green: CGFloat (GreenColor)/255.0 , blue: CGFloat(BlueColor)/255.0,alpha: CGFloat(alphalab)/255.0)**.CGColor**
    let colorBottom = UIColor(red: 35.0/255.0, green: 2.0/255.0, blue: 2.0/255.0, alpha: 1.0)**.CGColor**
    colorChange.tintColor = UIColor(red:CGFloat(RedColor)/255.0 , green: CGFloat (GreenColor)/255.0 , blue: CGFloat(BlueColor)/255.0,alpha: CGFloat(alphalab)/255.0)

    gradientLayer.colors = [colorTop, colorBottom]
    gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.locations = [ 0.0, 1.0]

    self.shadowview.layer.addSublayer(gradientLayer)

【讨论】:

以上是关于我想从渐变层的触摸点上色,怎么做?的主要内容,如果未能解决你的问题,请参考以下文章

AI实时上色的底色怎么变透明呢?

在drawInRect中绘制渐变层

渐变在拥抱脸模块中返回无

CSS3中如何实现渐变效果

边框渐变问题

求大神帮忙js图片渐变效果