使用手勢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];
}