CAGradientLayer + UIBezierPath 為檢視畫漸變背景色並帶有弧度
效果圖:
實現程式碼:
OC版本
xxx.h
// 處理CAGradientLayer + UIBezierPath 為檢視畫漸變背景色
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CornerGradientV : UIView
/**
CAGradientLayer + UIBezierPath 為檢視畫漸變背景色
@param frame 傳入的大小
@param colors 傳入的漸變顏色陣列
@param radius 需要裁剪的半徑
@return 處理好的CAGradientLayer + UIBezierPath 為檢視畫漸變背景色矩形
*/
- (instancetype)initWithFrame:(CGRect)frame withGradientColors:(NSArray *)colors withCornerRius:(CGFloat)radius;
/**
CAGradientLayer + UIBezierPath 為檢視畫漸變背景色
@param frame 傳入的大小
@param colors 傳入的漸變顏色陣列
@param radius 需要裁剪的半徑
@return 處理好的CAGradientLayer + UIBezierPath 為檢視畫漸變背景色矩形
*/
- (void)withFrame:(CGRect)frame withGradientColors:(NSArray *)colors withCornerRius:(CGFloat)radius;
@end
NS_ASSUME_NONNULL_END
@end
NS_ASSUME_NONNULL_END
xxx.m
/*
屬性說明:
frame : 漸變層的位置及大小
colors: 各個漸變範圍的顏色
startPoint 、endPoint: 漸變層起點 endPoint 漸變層終點 確定漸變的方向 如: (0,0)->(1,1) 左上到右下漸變 (0,0)->(0,1) 從上到下漸變 (0,0)->(1,0) 從左到右漸變
locations: 開始漸變的停止位置即非漸變色的位置,為遞增陣列,預設會根據顏色個數均勻分佈,如三個顏色預設陣列為@[0,0.5,1],即在0處為紅色0.5處為綠色1處為橘色,之間為漸變色 NSNumber 物件陣列 每一個number的範圍為[0,1]
type: 漸變方式,預設 kCAGradientLayerAxial軸向方式,也只有之一種方式
*/
#import "CornerGradientV.h"
@implementation CornerGradientV
- (instancetype)initWithFrame:(CGRect)frame withGradientColors:(NSArray *)colors withCornerRius:(CGFloat)radius {
self = [super initWithFrame:frame];
if (self) {
[self withFrame:frame withGradientColors:colors withCornerRius:radius];
}
return self;
}
- (void)withFrame:(CGRect)frame withGradientColors:(NSArray *)colors withCornerRius:(CGFloat)radius {
CAGradientLayer *gradLayer = [CAGradientLayer layer];
gradLayer.frame = frame;
gradLayer.colors = colors;
gradLayer.startPoint = CGPointMake(0, 1);
gradLayer.endPoint = CGPointMake(1, 1);
[self.layer addSublayer:gradLayer];
UIBezierPath *_circleSmallPath = [[UIBezierPath alloc] init];
[_circleSmallPath moveToPoint:CGPointMake(0, 0)];
[_circleSmallPath addLineToPoint:CGPointMake(0, frame.size.height - radius)];
[_circleSmallPath addCurveToPoint:CGPointMake(frame.size.width, self.frame.size.height - radius) controlPoint1:CGPointMake(frame.size.width / 2, frame.size.height) controlPoint2:CGPointMake(frame.size.width, frame.size.height - radius)];
[_circleSmallPath addLineToPoint:CGPointMake(frame.size.width, frame.size.height - radius)];
[_circleSmallPath addLineToPoint:CGPointMake(frame.size.width, 0)];
[_circleSmallPath closePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = _circleSmallPath.CGPath;
gradLayer.mask = shapeLayer;
}
@end
程式碼使用:
#define ZXHexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// CornerGradientV *vv = [[CornerGradientV alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 170)];
// [vv withFrame:vv.frame withGradientColors:@[(id)ZXHexRGB(0x6db831).CGColor,(id)ZXHexRGB(0x4d912a).CGColor] withCornerRius:20];
// [self.view addSubview:vv];
CornerGradientV *vvv = [[CornerGradientV alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 170) withGradientColors:@[(id)ZXHexRGB(0x6db831).CGColor,(id)ZXHexRGB(0x4d912a).CGColor] withCornerRius:20];
[self.view addSubview:vvv];
Swift版本
/// // 處理CAGradientLayer + UIBezierPath 為檢視畫漸變背景色
/*
屬性說明:
frame : 漸變層的位置及大小
colors: 各個漸變範圍的顏色
startPoint 、endPoint: 漸變層起點 endPoint 漸變層終點 確定漸變的方向 如: (0,0)->(1,1) 左上到右下漸變 (0,0)->(0,1) 從上到下漸變 (0,0)->(1,0) 從左到右漸變
locations: 開始漸變的停止位置即非漸變色的位置,為遞增陣列,預設會根據顏色個數均勻分佈,如三個顏色預設陣列為@[0,0.5,1],即在0處為紅色0.5處為綠色1處為橘色,之間為漸變色 NSNumber 物件陣列 每一個number的範圍為[0,1]
type: 漸變方式,預設 kCAGradientLayerAxial軸向方式,也只有之一種方式
*/
import UIKit
class CornetGradient_Swift: UIView {
/// CAGradientLayer + UIBezierPath 為檢視畫漸變背景色
///
/// - Parameters:
/// - frame: 傳入的大小
/// - colors: 傳入的漸變顏色陣列
/// - radius: 需要裁剪的半徑
init(frame: CGRect, withGradientColors colors: [Any]?, withCornerRius radius: CGFloat) {
super.init(frame: frame)
let gradLayer = CAGradientLayer()
gradLayer.frame = frame
gradLayer.colors = colors
gradLayer.startPoint = CGPoint(x: 0, y: 1)
gradLayer.endPoint = CGPoint(x: 1, y: 1)
layer.addSublayer(gradLayer)
let circleSmallPath = UIBezierPath()
circleSmallPath.move(to: CGPoint(x: 0, y: 0))
circleSmallPath.addLine(to: CGPoint(x: 0, y: frame.size.height - radius))
circleSmallPath.addCurve(to: CGPoint(x: frame.size.width, y: frame.size.height - radius), controlPoint1: CGPoint(x: frame.size.width / 2, y: frame.size.height), controlPoint2: CGPoint(x: frame.size.width, y: frame.size.height - radius))
circleSmallPath.addLine(to: CGPoint(x: frame.size.width, y: frame.size.height - radius))
circleSmallPath.addLine(to: CGPoint(x: frame.size.width, y: 0))
circleSmallPath.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = circleSmallPath.cgPath
gradLayer.mask = shapeLayer
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
使用
let colorArray = [ #colorLiteral(red: 0.4274509804, green: 0.7215686275, blue: 0.1921568627, alpha: 1).cgColor,#colorLiteral(red: 0.3019607843, green: 0.568627451, blue: 0.1647058824, alpha: 1).cgColor]
let v = CornetGradient_Swift.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 170), withGradientColors: colorArray, withCornerRius: 20)
self.view.addSubview(v)
參考資源:
https://www.cnblogs.com/jaesun/p/CAGradientLayer--UIBezierPath-wei-shi-tu-hua-jian-.html
iOS UIBezierPath方法的使用 :
https://www.jianshu.com/p/6cf87ca48811
相關文章
- iOS CAGradientLayer漸變色iOS
- CAGradientLayer設定漸變色
- iOS CAGradientLayer顏色漸變iOS
- 利用CAGradientLayer自定義顏色漸變viewView
- jQuery背景色漸變動畫效果jQuery動畫
- CSS背景色漸變效果程式碼CSS
- canvas繪製帶有漸變效果的直線Canvas
- CSS並不簡單--帶你認識不一樣的漸變CSS
- 短視訊直播系統,QLinearGradient之畫漸變線
- 手把手教你畫圓錐漸變
- iOS 2D 畫圖 和 UIBezierPathiOSUI
- 短視訊軟體開發,各個圖示自帶漸變色效果
- python seaborn畫熱力圖,自定義顏色漸變Python
- Avalonia中的線性漸變畫刷LinearGradientBrush
- 為app實現漸變的遮罩效果APP遮罩
- 檢視錶並行度並行
- 刷物化檢視並行並行
- 小議視覺設計中的漸變視覺
- js漸變彈出視窗和關閉視窗效果JS
- OpenGL模型檢視變換、投影變換、視口變換模型
- 使用CAShapeLayer與UIBezierPath畫出想要的圖形UI
- Linux 檢視埠占用並Linux
- 檢視繫結變數變數
- css奇技淫巧-色彩漸變與動態漸變CSS
- CSS3線性漸變和徑向漸變CSSS3
- 學習並使用線性漸變linear-gradient
- CSS背景漸變CSS
- Swift 漸變 imageSwift
- CSS 背景漸變CSS
- css 字型漸變CSS
- 直播軟體搭建,漸變色按鈕帶陰影樣式
- iOS CAGradientLayer 使用iOS
- 檢視並殺死殭屍程式
- 檢視並ORACLE的編碼方式Oracle
- 帶你掌握如何檢視並讀懂昇騰平臺的應用日誌應用日誌
- 建議開發員少用帶錶連結的檢視(此檢視非物化檢視)
- Canvas畫圖-一個比想象中更騷氣的圓(漸變圓環)Canvas
- ECharts柱條漸變色設定以及常用漸變色Echarts