2014-08-30 181 views
2

下面的代码绘制了一个圆,我如何修改现有的代码来绘制一个三角形呢?绘制三角形iOS

_colorDotLayer = [CALayer layer]; 

    CGFloat width = self.bounds.size.width-6; 
    _colorDotLayer.bounds = CGRectMake(0, 0, width, width); 
    _colorDotLayer.allowsGroupOpacity = YES; 
    _colorDotLayer.backgroundColor = self.annotationColor.CGColor; 
    _colorDotLayer.cornerRadius = width/2; 
    _colorDotLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); 
+2

此方法对绘制三角形没有用处。你应该看看UIBezierPath来做到这一点。 – rdelmar 2014-08-30 01:19:49

+0

你也应该看看CGContext。 – user2548635 2014-08-30 01:22:58

+0

SO不是代码翻译服务。 – Abizern 2014-08-30 14:54:14

回答

3

实施例的代码,它是基于该SO Answer其中提请分:

@implementation TriangleView 

-(void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    int sides = 3; 
    double size = 100.0; 
    CGPoint center = CGPointMake(160.0, 100.0); 

    double radius = size/2.0; 
    double theta = 2.0 * M_PI/sides; 

    CGContextMoveToPoint(context, center.x, center.y-radius); 
    for (NSUInteger k=1; k<sides; k++) { 
     float x = radius * sin(k * theta); 
     float y = radius * cos(k * theta); 
     CGContextAddLineToPoint(context, center.x+x, center.y-y); 
    } 
    CGContextClosePath(context); 

    CGContextFillPath(context);   // Choose for a filled triangle 
    // CGContextSetLineWidth(context, 2); // Choose for a unfilled triangle 
    // CGContextStrokePath(context);  // Choose for a unfilled triangle 
} 

@end 

enter image description here

0

使用UIBezeierPaths

CGFloat radius = 20; 
CGMutablePathRef path = CGPathCreateMutable(); 
CGPathMoveToPoint(path, NULL, (center.x + bottomLeft.x)/2, (center.y + bottomLeft.y)/2); 
CGPathAddArcToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomRight.x, bottomRight.y, radius); 
CGPathAddArcToPoint(path, NULL, bottomRight.x, bottomRight.y, center.x, center.y, radius); 
CGPathAddArcToPoint(path, NULL, center.x, center.y, bottomLeft.x, bottomLeft.y, radius); 
CGPathCloseSubpath(path); 

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:path]; 
CGPathRelease(path); 
+0

这不是可执行代码。什么是'中心'?什么是'bottomLeft'? – Jeff 2015-06-16 19:36:56

10
@implementation TriangleView { 
    CAShapeLayer *_colorDotLayer; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 

    if (_colorDotLayer == nil) { 
     _colorDotLayer = [CAShapeLayer layer]; 
     _colorDotLayer.fillColor = self.annotationColor.CGColor; 
     [self.layer addSublayer:_colorDotLayer]; 
    } 

    CGRect bounds = self.bounds; 
    CGFloat radius = (bounds.size.width - 6)/2; 
    CGFloat a = radius * sqrt((CGFloat)3.0)/2; 
    CGFloat b = radius/2; 
    UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path moveToPoint:CGPointMake(0, -radius)]; 
    [path addLineToPoint:CGPointMake(a, b)]; 
    [path addLineToPoint:CGPointMake(-a, b)]; 
    [path closePath]; 
    [path applyTransform:CGAffineTransformMakeTranslation(CGRectGetMidX(bounds), CGRectGetMidY(bounds))]; 
    _colorDotLayer.path = path.CGPath; 
} 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    self.annotationColor = [UIColor redColor]; 
} 

@end 

结果:

triangle view

15

虽然显示几个核芯显卡的解决方案,我想补充一个基于Core Animation的解决方案。基于我的博客文章

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIBezierPath* trianglePath = [UIBezierPath bezierPath]; 
    [trianglePath moveToPoint:CGPointMake(0, view3.frame.size.height-100)]; 
    [trianglePath addLineToPoint:CGPointMake(view3.frame.size.width/2,100)]; 
    [trianglePath addLineToPoint:CGPointMake(view3.frame.size.width, view2.frame.size.height)]; 
    [trianglePath closePath]; 

    CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer]; 
    [triangleMaskLayer setPath:trianglePath.CGPath]; 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, size.width, size.height)]; 

    view.backgroundColor = [UIColor colorWithWhite:.75 alpha:1]; 
    view.layer.mask = triangleMaskLayer; 
    [self.view addSubview:view]; 
} 

代码:http://blog.vikingosegundo.de/2013/10/01/hittesting-done-right/

+0

此网页不可用 – 2015-07-02 08:06:57

+0

我想知道为什么这不起作用。 – esh 2015-07-22 06:56:15

+0

如果视图需要调整大小以适合屏幕,这是非常普遍的(因为它以故事板中使用的任意大小出现)。您需要在布局阶段设置路径(如[本答案](http://stackoverflow.com/a/25578729/77567)),或者使用基于drawRect:的解决方案,如[this one]( http://stackoverflow.com/a/25578212/77567)与内容模式重绘或缩放到适合。 – 2016-03-10 13:53:51

1

我觉得比这个解决方案更容易:

UIBezierPath *path = [UIBezierPath new]; 
[path moveToPoint:(CGPoint){20, 0}]; 
[path addLineToPoint:(CGPoint){40, 40}]; 
[path addLineToPoint:(CGPoint){0, 40}]; 
[path addLineToPoint:(CGPoint){20, 0}]; 

// Create a CAShapeLayer with this triangular path 
// Same size as the original imageView 
CAShapeLayer *mask = [CAShapeLayer new]; 
mask.frame = self.viewTriangleCallout.bounds; 
mask.path = path.CGPath; 

enter image description here

这是我的白色三角: 您可以更改点或旋转,如果你想:

UIBezierPath *path = [UIBezierPath new]; 
[path moveToPoint:(CGPoint){0, 0}]; 
[path addLineToPoint:(CGPoint){40, 0}]; 
[path addLineToPoint:(CGPoint){20, 20}]; 
[path addLineToPoint:(CGPoint){0, 0}]; 

// Create a CAShapeLayer with this triangular path 
// Same size as the original imageView 
CAShapeLayer *mask = [CAShapeLayer new]; 
mask.frame = self.viewTriangleCallout.bounds; 
mask.path = path.CGPath; 

enter image description here