• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 UIView 中的贝塞尔路径内绘制/绘制草图颜色

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:44:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在 UIView 上的 UIBezier 路径的帮助下绘制形状层:

    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame=CGRectMake(-view.frame.origin.x, -view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blackColor] CGColor];
    pathLayer.fillColor = [[UIColor clearColor] CGColor];
    //pathLayer.fillColor = nil;
    pathLayer.lineWidth = 6.0;
    pathLayer.lineJoin = kCALineJoinBevel;
    [view.layer addSublayer:pathLayer];

在我想在那个 UIView 上绘制的接触点上,我正在这样做:

-(void)touchesBeganNSSet *)touches withEventUIEvent *)event
{
      UITouch *touch=[touches anyObject];
      CGPoint touchBeganPoint;
      touchBeganPoint=[touch locationInView:self];
      actionString=@"drawPencil";
      previousPoint1 = [touch locationInView:self];
      previousPoint2 = [touch locationInView:self];
      currentPoint = [touch locationInView:self];
      [self touchesMoved:touches withEvent:event];
}

-(void)touchesMovedNSSet *)touches withEventUIEvent *)event
{
      UITouch *touch = [touches anyObject];
      previousPoint2  = previousPoint1;
      previousPoint1  = currentPoint;
      currentPoint    = [touch locationInView:self];
      [self calculateMinImageArea:previousPoint1 :previousPoint2 :currentPoint];
}

CGPoint midPoint(CGPoint p1, CGPoint p2) //helper function
{
    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}

- (void)calculateMinImageAreaCGPoint)pp1 CGPoint)pp2 CGPoint)cp
{
      // calculate mid point
      CGPoint mid1    = midPoint(pp1, pp2);
      CGPoint mid2    = midPoint(cp, pp1);

      CGMutablePathRef path = CGPathCreateMutable();
      CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
      CGPathAddQuadCurveToPoint(path, NULL, pp1.x, pp1.y, mid2.x, mid2.y);
      CGRect bounds = CGPathGetBoundingBox(path);
      CGPathRelease(path);

      CGRect drawBox = bounds;

      //Pad our values so the bounding box respects our line width
      drawBox.origin.x        -= 20 * 1.5;
      drawBox.origin.y        -= 20 * 1.5;
      drawBox.size.width      += 20 * 2.5;
      drawBox.size.height     += 20 * 2.5;

      UIGraphicsBeginImageContextWithOptions(drawBox.size, NO, 0);
      [self.layer renderInContext:UIGraphicsGetCurrentContext()];
      curImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

      // generate the if conditionover here and than check and draw over paths
      [self setNeedsDisplayInRect:drawBox];
      [[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate: [NSDate date]];
}

-(void)drawRectCGRect)rect
{
      CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
      CGPoint mid2 = midPoint(currentPoint, previousPoint1);
      [curImage drawAtPoint:CGPointMake(0, 0)];
      CGContextRef context = UIGraphicsGetCurrentContext();
      CALayer *layer=[self.layer.sublayers objectAtIndex:0];
      [self.layer renderInContext:context];
      CGContextMoveToPoint(context, mid1.x, mid1.y);
      CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
      CGContextSetLineCap(context, kCGLineCapRound);
      CGContextSetBlendMode(context, kCGBlendModeNormal);
      CGContextSetLineJoin(context, kCGLineJoinRound);
      CGContextSetLineWidth(context, 20);
      CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
      CGContextSetShouldAntialias(context, YES);
      CGContextSetAllowsAntialiasing(context, YES);
      CGContextSetFlatness(context, 1.f);
      CGContextSetAlpha(context,1.0);
      CGContextStrokePath(context);
      [super drawRect:rect];
}

现在当我在 self.layer 上绘图时,它仍然会影响添加的子层(形状层)。我只想在这条路径内绘制,不能使用 [path containspoint..] ,如绘制矩形,我正在绘制的图像有一定的宽度,有时会穿过路径或干扰其笔画。

实现此目的的一种方法是使用其子层(CAShapelayer)剪辑 UIView,然后在 uiview 上再次绘制它(这将清除形状图层区域之外的背景)。但是找不到如何使用 CAShapeLayer 剪辑它。

任何关于如何顺利实现这一目标的建议或见解都可能会有很大帮助。 谢谢!



Best Answer-推荐答案


拍摄另一张图像,该图像是具有所需路径的 View 快照,并填充了选定的颜色。 比触摸移动称为 drawrect 具有相同的代码,除了 [UIColor colorWithPatternImage:img] 作为上下文的颜色有一点异常(exception)。 Drawing in UIView on touch points gives blurr drawing effect on boundaries

关于ios - 在 UIView 中的贝塞尔路径内绘制/绘制草图颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30658974/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap