【问题标题】:How to draw irregular shaped rectangle and fill out the color with four pin points in ios如何在ios中绘制不规则形状的矩形并用四个针点填充颜色
【发布时间】:2013-08-08 07:28:29
【问题描述】:

我目前正在开发像“kresta app”这样的应用程序。首先我从照片库中挑选图像。接下来我的工作是用户可以选择他想要应用百叶窗和阴影的区域。所以我想要做的是我有四个引脚,用户可以触摸和拖动引脚以进行区域选择。我用下面的代码实现了这个逻辑。

jpg

在touches中我调用了这个方法

 UIBezierPath *aPath = [UIBezierPath bezierPath];

// Set the starting point of the shape.
   [aPath moveToPoint:pinImageView1.center];
// Draw the lines.
[aPath addLineToPoint:pinImageView2.center];
[aPath addLineToPoint:pinImageView3.center];
[aPath addLineToPoint:pinImageView4.center];
[aPath closePath];

CAShapeLayer *square = [CAShapeLayer layer];
square.path = aPath.CGPath;
[pickedImageView.layer addSublayer:square];

我的问题是每次它添加一个图层。我如何实现这个逻辑?有什么方法可以删除前一层并更新新层?或者我的方法不对,如果错了,请提出任何其他方法来实现这个逻辑。

【问题讨论】:

    标签: iphone ios image-processing uiimageview uiimage


    【解决方案1】:

    如果您在类属性中保存对此CAShapeLayer 的引用,则可以简单地一次又一次地更新其path 属性,它会自动更新视图层上的子层。所以,addSublayer 一次,然后在以后,只要您想在屏幕上反映覆盖图像的形状变化,只需更新此 CAShapeLayerpath

    【讨论】:

    • @banu 您的方式对于初始添加图层是正确的,但对于后续更新,您只需更新路径,不要创建新的CAShapeLayer。而且我也看不到您在哪里指定填充颜色(例如square.fillColor = [[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:0.2] CGColor]; 或其他)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    相关资源
    最近更新 更多