使用CAShapeLayer與UIBezierPath畫出想要的圖形
步驟:
1、新建UIBezierPath物件bezierPath
2、新建CAShapeLayer物件caShapeLayer
3、將bezierPath的CGPath賦值給caShapeLayer的path,即caShapeLayer.path = bezierPath.CGPath
4、把caShapeLayer新增到某個顯示該圖形的layer中
下面的小例子是一個環形的progress程式碼,有具體的使用方法
.h檔案:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface KACircleProgressView : UIView {
CAShapeLayer *_trackLayer;
UIBezierPath *_trackPath;
CAShapeLayer *_progressLayer;
UIBezierPath *_progressPath;
}
@property (nonatomic, strong) UIColor *trackColor;
@property (nonatomic, strong) UIColor *progressColor;
@property (nonatomic) float progress;//0~1之間的數
@property (nonatomic) float progressWidth;
- (void)setProgress:(float)progress animated:(BOOL)animated;
@end
.m檔案:
#import "KACircleProgressView.h"
@implementation KACircleProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_trackLayer = [CAShapeLayer new];
[self.layer addSublayer:_trackLayer];
_trackLayer.fillColor = nil;
_trackLayer.frame = self.bounds;
_progressLayer = [CAShapeLayer new];
[self.layer addSublayer:_progressLayer];
_progressLayer.fillColor = nil;
_progressLayer.lineCap = kCALineCapRound;
_progressLayer.frame = self.bounds;
//預設5
self.progressWidth = 5;
}
return self;
}
- (void)setTrack
{
_trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];;
_trackLayer.path = _trackPath.CGPath;
}
- (void)setProgress
{
_progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/ 2 startAngle:- M_PI_2 endAngle:(M_PI * 2) * _progress - M_PI_2 clockwise:YES];
_progressLayer.path = _progressPath.CGPath;
}
- (void)setProgressWidth:(float)progressWidth
{
_progressWidth = progressWidth;
_trackLayer.lineWidth = _progressWidth;
_progressLayer.lineWidth = _progressWidth;
[self setTrack];
[self setProgress];
}
- (void)setTrackColor:(UIColor *)trackColor
{
_trackLayer.strokeColor = trackColor.CGColor;
}
- (void)setProgressColor:(UIColor *)progressColor
{
_progressLayer.strokeColor = progressColor.CGColor;
}
- (void)setProgress:(float)progress
{
_progress = progress;
[self setProgress];
}
- (void)setProgress:(float)progress animated:(BOOL)animated
{
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
使用:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
KACircleProgressView *progress = [[KACircleProgressView alloc] initWithFrame:CGRectMake(20, 40, 100, 100)];
[self.view addSubview:progress];
progress.trackColor = [UIColor blackColor];
progress.progressColor = [UIColor orangeColor];
progress.progress = 0.7;//progress所佔比例
progress.progressWidth = 10;//圖形圓的粗細
}
效果圖:
相關文章
- 放肆的使用 UIBezierPath 和 CAShapeLayer 畫各種圖形UI
- UIBezierPath+CAShapeLayer繪製微信聊天圖片效果UI
- 2.2 畫出函式圖形函式
- iOS動畫系列之八:使用CAShapeLayer繪畫動態流量圖iOS動畫
- iOS 2D 畫圖 和 UIBezierPathiOSUI
- css 畫圖形大全CSS
- 畫影圖形: SVG & Canvas 圖形對比SVGCanvas
- 箱形圖(python畫圖)Python
- Canvas 畫圖 - 滑鼠移動圖形Canvas
- 精彩:JS畫的圖形[Good篇] (轉)JSGo
- DrawPad 圖形繪畫工具
- DrawPad圖形繪畫工具
- MongoDB圖形化工具的使用與java操作MongoDBJava
- UIBezierPath的使用方法UI
- 精彩:JS畫的圖形[金剛石篇] (轉)JS
- DrawPad for mac 圖形繪畫工具Mac
- DrawPad for mac圖形繪畫工具Mac
- mac圖形繪畫工具:DrawPadMac
- VNC圖形介面操作Linux時出現灰色畫面問題VNCLinux
- 使用Excel畫出各類統計圖(3)Excel
- 使用SAPGUI畫圖GUI
- 純CSS畫的基本圖形(矩形、圓形、三角形、多邊形、愛心、八卦CSS
- 使用JavaScript進行基本圖形操作與處理JavaScript
- Viso的物件圖形複製到word,發現圖形畫布底部有大量空白,如何解決物件
- Vue 你想要的流程圖Vue流程圖
- UIBezierPathUI
- Bresenham畫線演算法(計算機圖形學)演算法計算機
- 畫出Android的大體架構圖Android架構
- 如何在論文中畫出漂亮的插圖?
- 使用css繪製圖形CSS
- 使用css 畫三角形的三種方法CSS
- 遊戲原畫教程:角色設計中的幾個基本圖形的用法遊戲
- AI 畫圖真刺激,手把手教你如何用 ComfyUI 來畫出刺激的圖AIUI
- 設計初學者必學之AI圖形描邊:寬度工具與畫筆工具AI
- CSS畫出半圓,四分之一圓,三角等圖形CSS
- 字母圖形(字元與ASCII碼)字元ASCII
- 蒸汽波風格圖不會畫?AI作畫幫你秒級出圖AI
- Java圖形化:Swing表格的使用Java