UIBezierPath, UIPanGestureRecognizer 高效繪制圖形

使用手勢UIPanGestureRecognizer作為手指在屏幕上滑動(dòng)獲取坐標(biāo)方式,摒棄touches:begin; touches:moved。

// 初始化設(shè)置

- (void)setUp

{

? ? // 添加pan手勢

? ? _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

? ? _pan.delegate=self;

? ? _pan.maximumNumberOfTouches = 1;

? ? [self addGestureRecognizer:_pan];


? ? _lineWidth = 1;

? ? _pathColor= [UIColorblackColor];

}


UIPanGestureRecognizer方法

// 當(dāng)手指拖動(dòng)的時(shí)候調(diào)用

- (void)pan:(UIPanGestureRecognizer *)pan

{


? ? // 獲取當(dāng)前手指觸摸點(diǎn)

? ? CGPointcurP = [panlocationInView:self];

? ? CGPointmidPoint =midpoint(_prePoint, curP);

? ? // 獲取開始點(diǎn)

? ? if (pan.state == UIGestureRecognizerStateBegan) {

? ? ? ? // 創(chuàng)建貝瑟爾路徑

? ? ? ? _path= [[WDDrawPathalloc]init];


? ? ? ? // 設(shè)置線寬

? ? ? ? _path.lineWidth = _lineWidth;


? ? ? ? // 給路徑設(shè)置顏色

? ? ? ? _path.pathColor = _pathColor;


? ? ? ? // 設(shè)置路徑的起點(diǎn)

? ? ? ? [_pathmoveToPoint:curP];

? ? ? ? _startPoint= curP;


? ? ? ? // 保存描述好的路徑

? ? ? ? [self.pathsaddObject:_path];

? ? }

? ? if (pan.state==UIGestureRecognizerStateEnded) {

? ? ? ? _endPoint= curP;

? ? }

? ? if (pan.state==UIGestureRecognizerStateChanged) {

? ? ? ? if(self.viewType==kQuXian) {

? ? ? ? ? ? [_path addQuadCurveToPoint:midPoint controlPoint:_prePoint];

? ? ? ? }elseif(self.viewType==kZhiXian) {

? ? ? ? ? ? [_pathremoveAllPoints];

? ? ? ? ? ? [_pathmoveToPoint:_startPoint];

? ? ? ? ? ? [_pathaddLineToPoint:_endPoint];

? ? ? ? }elseif(self.viewType==kJuXing) {

? ? ? ? ? ? [_pathremoveAllPoints];

? ? ? ? ? ? [_pathmoveToPoint:_startPoint];

? ? ? ? ? ? CGPointleftBottomPoint =CGPointMake(_startPoint.x, curP.y);

? ? ? ? ? ? CGPointrightTopPoint =CGPointMake(curP.x,_startPoint.y);

? ? ? ? ? ? [_pathaddLineToPoint:leftBottomPoint];

? ? ? ? ? ? [_pathaddLineToPoint:curP];

? ? ? ? ? ? [_pathaddLineToPoint:rightTopPoint];

? ? ? ? ? ? [_pathaddLineToPoint:_startPoint];

? ? ? ? }

? ? ? ? _endPoint= curP;

? ? }

? ? // 手指一直在拖動(dòng)

? ? // 添加線到當(dāng)前觸摸點(diǎn)

? ? _prePoint= curP;

? ? // 重繪

? ? [self setNeedsDisplay];


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容