美文网首页
UIBezierPath, UIPanGestureRecogn

UIBezierPath, UIPanGestureRecogn

作者: 唐朝小农民 | 来源:发表于2020-06-02 17:20 被阅读0次

使用手势UIPanGestureRecognizer作为手指在屏幕上滑动获取坐标方式,摒弃touches:begin; touches:moved。

// 初始化设置

- (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方法

// 当手指拖动的时候调用

- (void)pan:(UIPanGestureRecognizer *)pan

{

    // 获取当前手指触摸点

    CGPointcurP = [panlocationInView:self];

    CGPointmidPoint =midpoint(_prePoint, curP);

    // 获取开始点

    if (pan.state == UIGestureRecognizerStateBegan) {

        // 创建贝瑟尔路径

        _path= [[WDDrawPathalloc]init];

        // 设置线宽

        _path.lineWidth = _lineWidth;

        // 给路径设置颜色

        _path.pathColor = _pathColor;

        // 设置路径的起点

        [_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;

    }

    // 手指一直在拖动

    // 添加线到当前触摸点

    _prePoint= curP;

    // 重绘

    [self setNeedsDisplay];

}

相关文章

网友评论

      本文标题:UIBezierPath, UIPanGestureRecogn

      本文链接:https://www.haomeiwen.com/subject/rrskzhtx.html