ios - Objective-C 仅在触摸时填充特定区域的颜色

标签 ios objective-c colors

我已经尝试和搜索了很多,但没有找到解决方案。

我有这张图片的 .png [1]: http://imm.io/1f3DY

如果用户触摸 f.e.在区域 1

我想用颜色填充这个区域。蓝色。

有什么方法可以在代码中找到/创建这些区域并用颜色填充它吗?

我已将 .png 转换为 .svg 并创建了一些贝塞尔路径。将贝塞尔路径存储在数组中并添加手势识别器。这很好用。找到了我所触及的正确道路。可以改变路径的颜色。但只有黑线。我想填补 f.e. 之间的空白。路径 1 和路径 2。我该怎么做?

       //The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    CGPoint location = [recognizer locationInView:[recognizer.view superview]];

    location.y = self.view.bounds.size.height - location.y;
    for(int i = 0; i< bezierPathes.count; i++)
    {
          //if([b containsPoint:location])

        UIBezierPath* b = bezierPathes[i];
        if([[self tapTargetForPath:b] containsPoint:location])
        {
            [[UIColor redColor] setFill];
            [b fill];
            [testView setNeedsDisplay];

        }

    }
}

// this method will let you easily select a bezier path ( 15 px up and down of a path drawing)
- (UIBezierPath *)tapTargetForPath:(UIBezierPath *)path
{
    if (path == nil) {
        return nil;
    }

    CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(35.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
    if (tapTargetPath == NULL) {
        return nil;
    }

    UIBezierPath *tapTarget = [UIBezierPath bezierPathWithCGPath:tapTargetPath];
    CGPathRelease(tapTargetPath);
    return tapTarget;
}

最佳答案

您还可以使用洪水填充算法来执行此操作。

听到的是链接http://en.wikipedia.org/wiki/Flood_fill

关于ios - Objective-C 仅在触摸时填充特定区域的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18191993/

相关文章:

ios - 如果未点击按钮则结束游戏 - Swift

java - 更改java控制台输出的颜色

language-agnostic - 为功率计生成红色和绿色之间的颜色?

objective-c - 弱和强属性示例

android - 如何在 Android 应用程序中自定义顶部状态栏?

ios - 使用 Firebase Auth 进行身份验证时注销 Facebook 用户

ios - 只有当核心数据和 NSFetchedResultsController 满足某些条件时,才在 UITableViewCell 中实现 UIImageView

iphone - 如何获取tableView :(UITableView *)tableView heightForRowAtIndexPath function?中的cell对象

objective-c - 将 tabelview indexPath.row 作为字符串传递给 VC WebView

ios - 将简单的动画 GIF 添加到 iOS Spritekit 游戏?