ios - 使用 CAGradientLayer 定义渐变的角度

标签 ios objective-c gradient linear-gradients cagradientlayer

我正在尝试使用 CaGradientLayer 绘制角度渐变。我知道可以使用 startPointendPoint 定义角度。我可以计算一些标准角度的这些点,如 0、90、180、360 等。但我想为任意角度制定这些点。我曾尝试使用一些三角函数来计算它,但没有取得任何成功。谁能告诉我如何计算任意角度的这些点?

最佳答案

swift 3

static func setGradient(view: UIView!,viewRadius: CGFloat!, color1: UIColor!, color2: UIColor!, angle: Double!, alphaValue: CGFloat!){
    let gradient = CAGradientLayer()
    
    gradient.frame =  CGRect(origin: CGPoint.zero, size: view.frame.size)
    
    gradient.colors = [color1.withAlphaComponent(alphaValue).cgColor, color2.withAlphaComponent(alphaValue).cgColor]
    let x: Double! = angle / 360.0
    let a = pow(sinf(Float(2.0 * M_PI * ((x + 0.75) / 2.0))),2.0);
    let b = pow(sinf(Float(2*M_PI*((x+0.0)/2))),2);
    let c = pow(sinf(Float(2*M_PI*((x+0.25)/2))),2);
    let d = pow(sinf(Float(2*M_PI*((x+0.5)/2))),2);
    
    gradient.endPoint = CGPoint(x: CGFloat(c),y: CGFloat(d))
    gradient.startPoint = CGPoint(x: CGFloat(a),y:CGFloat(b))
    
    view.roundCorners([.topLeft, .bottomLeft], radius: viewRadius)
    view.layer.insertSublayer(gradient, at: 0)
    
    
}

关于ios - 使用 CAGradientLayer 定义渐变的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26886665/

相关文章:

objective-c - 在 NSArray 上使用 valueForKeyPath,如果属性设置为 YES,如何计算 BOOL 属性

ios - 如何解决 Google Fabrics 崩溃 - 致命异常 : DeviceNotSupported?

css - 使渐变边框看起来像一条线

Chrome 中的 CSS 渐变条纹

ios - 快速检查类是否符合协议(protocol)始终为 true

ios - 如何从 View 中显示 View Controller ?

ios - UIPickerView pickerView :titleForRow:rowforComponent method not called

ios - 在 Interface Builder 中为渐变 View 设置渐变背景颜色

objective-c - iOS 中的 XOR 文件加密

ios - Swift:如何根据宽度排列按钮?间距都相等吗?