- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在为 iPhone 创建一个拼图游戏。
这里我必须像下图一样裁剪图像
在谷歌搜索之后,我开始知道 uibezier 路径是裁剪不规则形状图像的最佳路径。
但我没有得到任何代码或想法如何像上面那样裁剪图像。
最佳答案
这需要大量的试验和错误,我很快就这样做了,只是为了让您了解如何使用贝塞尔路径创建形状。下面是我快速创建的用于创建正方形的示例代码
UIBezierPath *aPath = [UIBezierPath bezierPath];
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(50.0, 50.0)];
// Draw the lines.
[aPath addLineToPoint:CGPointMake(100.0, 50.0)];
[aPath addLineToPoint:CGPointMake(100.0, 100.0)];
[aPath addLineToPoint:CGPointMake(50.0, 100.0)];
[aPath closePath];
CAShapeLayer *square = [CAShapeLayer layer];
square.path = aPath.CGPath;
[self.view.layer addSublayer:square];
如果我有更多时间,我可以创建图像,但很快就完成了,因为没有太多时间。一旦你了解了这些点是如何产生的,它就不会太难使用。创建此形状需要大量试验和错误,但使用我提供的代码作为如何使用贝塞尔路径创建形状的基础。您将需要创建更多的点才能最终得到您想要的形状。
我还建议您查看有关创建不规则形状的 Apple 开发人员指南
请特别查看“将曲线添加到您的路径”以了解如何创建曲线并将它们添加到您的图像中。您将需要它来创建您要创建的拼图 block 形状
编辑:
试试这个方法
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
if (![[imgView layer] mask])
[[imgView layer] setMask:[CAShapeLayer layer]];
[(CAShapeLayer*) [[imgView layer] mask] setPath:[clippingPath CGPath]];
}
上述方法将采用贝塞尔曲线路径和 ImageView,然后将贝塞尔曲线路径应用于该特定 imageView。它也会进行剪裁。将需要大量的试验和错误,我想得到恰到好处的形状,有时创建复杂的形状会很困难和令人沮丧。
应用此代码的快速示例
UIBezierPath *aPath = [UIBezierPath bezierPath];
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(0.0, 0.0)];
// Draw the lines.
[aPath addLineToPoint:CGPointMake(50.0, 0.0)];
[aPath addLineToPoint:CGPointMake(50.0, 50.0)];
[aPath addLineToPoint:CGPointMake(0.0, 50.0)];
[aPath closePath];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar.png"]];
imgView.frame = CGRectMake(0, 0, 100, 100);
[self setClippingPath:aPath :imgView];
[self.view addSubview:imgView];
只是快速地从图像的左上部分制作了一个正方形。例如,如果您有一个方形图像,您可以遍历图像的宽度和高度,使用上面的代码裁剪成单独的正方形并单独返回它们。创建拼图 block 要复杂得多,但希望这对您有所帮助
关于iphone UIbezierpath 不规则图像裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12670051/
negExpression : (NOT^)* primitiveElement ; 是我的规矩。我现在有这个代码: !!(1==1) 我希望我最终会得到这棵树: NOT | NOT
我遇到以下问题,我正在创建一个作为预算副本的表单,但这种类型的预算不包含增值税%,并且商品不会通过会计。 问题如下我创建了一个名为budget.table的模型如下: class TableEleme
我对 Java 相当陌生,但对一般编程不太熟悉。我在 Windows Vista 上使用 Java 1.7.0_07。我正在尝试弄清楚如何使 Swing Timer 定期计时。 我注意到,即使我设置了
我有一个静态站点,它突然显示不规则的标题。这是一个包含大量 JavaScript 的单一页面,包括页面顶部的表格选择。该网站六个月前运行良好。现在,我在 12 个不同的导航选项卡中的一半上看到无法解释
在我参加的 CS 类(class)中,有一个不规则语言的例子: {a^nb^n | n >= 0} 我可以理解它是不规则的,因为没有有限状态自动机/机器可以编写来验证和接受此输入,因为它缺少内存组件。
给定以下高频但稀疏的时间序列: #Sparse Timeseries dti1 = pd.date_range(start=datetime(2015,8,1,9,0,0),periods=10,fr
我有 X、Y、Z 格式的数据,其中所有数据都是一维数组,Z 是坐标 (X,Y) 处的测量幅度。我想将此数据显示为等高线或“imshow”图,其中等高线/颜色代表 Z 值(幅度)。 用于测量和 X 和
这是 Stackoverflow 上的一个递归问题,但给出的解决方案 here仍然不完美。对我来说,屈服仍然是 python 中最复杂的东西之一,所以我不知道如何自己修复它。 当给定函数的任何列表中的
我使用 PHP 5.3.3 在 RHEL 6 服务器上部署了一个 symfony 1.4 项目。我不定期地在 php 错误日志中收到条目,提示找不到 sfProjectConfiguration 并且
我是一名优秀的程序员,十分优秀!