objective-c - 带渐变填充的 Delaunay 三角剖分?

标签 objective-c ipad uikit

我正在尝试重新创建类似这种形状的东西 - http://www.creativeapplications.net/wp-content/uploads/2010/09/horizon_02_1024x768.png - 在 iOS 上使用 UIKit 或 CoreGraphics。

图中的这个是用 openFrameworks 完成的,它确实可以在 iOS 上运行。不过,我想在不使用 oF 的情况下构建相同的内容,这样我就可以将其全部保留为原生。

我假设该形状是通过 Delaunay 三角测量创建的,我可以在 Objective-C 中执行此操作。问题是用渐变而不是普通的 UIColor 填充形状!

有没有一种方法可以在 iPad 上以良好的性能本地完成类似的事情,或者我应该使用 openFrameworks 来完成它并将其作为一个层引入到应用程序中?

最佳答案

你是问如何用渐变填充三角形吗?下面的代码就是这样做的。要使其正常工作,您需要添加 QuartzCore 框架,并且

#import <QuartzCore/QuartzCore.h>

代码创建一个三角形的形状图层,然后将其用作渐变图层的 mask 。希望这会有所帮助。

- (void)drawRect:(CGRect)rect
{
    CGFloat w = self.bounds.size.width / 4;
    CGFloat h = self.bounds.size.height / 4;
    CGFloat x = w * 2;
    CGFloat y = h;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path,NULL, x, y);
    CGPathAddLineToPoint(path, NULL, x+w, y+2*h);
    CGPathAddLineToPoint(path, NULL, x-w, y+2*h);
    CGPathCloseSubpath(path);


    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    [shapeLayer setPath:path];

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    gradientLayer.startPoint = CGPointMake(0.25, 1.0);
    gradientLayer.endPoint = CGPointMake(0.75, 0.0);
    UIColor *startColour = [UIColor redColor];
    UIColor *endColour = [UIColor greenColor];
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[startColour CGColor], (id)[endColour CGColor], nil];

    [gradientLayer setMask:shapeLayer];

    [self.layer addSublayer:gradientLayer];

    CGPathRelease(path);
}

关于objective-c - 带渐变填充的 Delaunay 三角剖分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075960/

相关文章:

css - iPad 上的背景图像宽度不是 100%

ios - 使属于 UIView 子类的计时器无效

android - 从原生访问插件的sqlite数据库

objective-c - 更改标准“关于”面板的大小

ios - Crittercism更新到5.6.2后出现编译错误 "Could not build module ' Crittercism'”

iphone - 观察 NSURL iCloud 属性

ios - UITableview NSarray isEqualToString 无法识别的选择器发送到实例

iphone - 如何使用 Apple 钥匙串(keychain)?

ios - UIImageView 溢出 Collection View 单元格

ios - UIStackView - 拖动以重新排列排列的 subview ?