My client wants the background View with this gradient effect background Gradient : rgb(118,118,118) | #ffffff | rgb(198,198,197) linear left to right I have tried this way but it is happening in Vertical direction I want it in Horizontal way
UIColor *leftColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0];
UIColor *middleColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
UIColor *rightColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:197.0/255.0 alpha:1.0];
// Create the gradient
CAGradientLayer *theViewGradient = [CAGradientLayer layer];
theViewGradient.colors = [NSArray arrayWithObjects: (id)leftColor.CGColor, (id)middleColor.CGColor,(id)rightColor.CGColor, nil];
theViewGradient.frame = self.view.bounds;
//Add gradient to view
[self.view.layer insertSublayer:theViewGradient atIndex:0];
Like this ??
To use gradients, you first need a Container widget, and within that you need to access its decoration property. Start by building the decoration of the Container widget in your _MyHomePageState build method in main. dart .
Linear Gradient Creating a layer with gradient colors is quite simple. For the most basic gradient, it only requires the colors you want to use, and then you can optionally adjust color location. Once you've created your gradient, add it simply to your view layer by calling the addSublayer function.
In Swift 3.0 & 4.0, to go from left to right:
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
enum GradiantDirection {
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}
class func setGradiantColor(view : UIView, topColor : UIColor, bottomColor:UIColor, cornerRadius : CGFloat = 0.0,gradiantDirection : GradiantDirection = .topToBottom )
{
view.layer.sublayers?.filter{ $0 is CAGradientLayer }.forEach{ $0.removeFromSuperlayer() }
let gradient: CAGradientLayer = CAGradientLayer()
gradient.colors = [topColor.cgColor,bottomColor.cgColor]
gradient.frame = view.bounds
switch gradiantDirection {
case .topToBottom:
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
case .bottomToTop:
gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
case .leftToRight:
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
case .rightToLeft:
gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
}
gradient.masksToBounds = true
let gradientLayer = CAGradientLayer()
gradientLayer.cornerRadius = cornerRadius
gradient.rasterizationScale = 100
view.layer.insertSublayer(gradient, at: 0)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With