2016-09-26 50 views
0

我想從漸變圖層的接觸點着色。想從漸變層的觸點着色,怎麼樣?

我嘗試這樣:

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  
} 

梯度層:

enter image description here

回答

0

的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)