2013-02-18 38 views
1

我想画从一个点(的UIView中心)始发线,像这样:UIBezierPath线期一脉相承

- (void)drawRect:(CGRect)rect { 
    UIBezierPath *path = [self createPath]; 
    [path stroke]; 

    path = [self createPath]; 
    CGAffineTransform rot = CGAffineTransformMakeRotation(2 * M_PI/16); 
    [path applyTransform:rot]; 
    [path stroke]; 

    path = [self createPath]; 
    rot = CGAffineTransformMakeRotation(2 * M_PI/8); 
    [path applyTransform:rot]; 
    [path stroke]; 
} 

- (UIBezierPath *) createPath { 
    UIBezierPath *path = [UIBezierPath bezierPath]; 
    CGPoint start = CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f); 
    CGPoint end = CGPointMake(start.x + start.x/2, start.y); 
    [path moveToPoint:start]; 
    [path addLineToPoint:end]; 
    return path; 
} 

的想法是画在同一行,并应用旋转(约该中心=该行的开始)。结果是这样的:

https://dl.dropbox.com/u/103998739/bezierpath.png

两个旋转的线条似乎在某种程度上也抵消。图层定位点默认为0.5/0.5。 我在做什么错?

+0

请让我知道如果你需要了解更多信息! - 否则,请注意,您可以通过单击复选标记来“接受”答案。这标志着问题已经解决,并为您和答案的作者提供了一些声誉点。 – 2013-07-13 13:40:37

回答

1

在iOS中,默认坐标系原点位于图层的左上角。 (anchorpoint与图层及其超级图层之间的关系相关,但不适用于图层内的坐标系。)

要将坐标系的原点移动到图层的中心,可以应用平移第一:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(context, self.bounds.size.width/2.0f, self.bounds.size.height/2.0f); 

    UIBezierPath *path = [self createPath]; 
    [path stroke]; 

    path = [self createPath]; 
    CGAffineTransform rot = CGAffineTransformMakeRotation(2 * M_PI/16); 
    [path applyTransform:rot]; 
    [path stroke]; 

    path = [self createPath]; 
    rot = CGAffineTransformMakeRotation(2 * M_PI/8); 
    [path applyTransform:rot]; 
    [path stroke]; 
} 

- (UIBezierPath *) createPath { 
    UIBezierPath *path = [UIBezierPath bezierPath]; 
    CGPoint start = CGPointMake(0, 0); 
    CGPoint end = CGPointMake(self.bounds.size.width/4.0f, 0); 
    [path moveToPoint:start]; 
    [path addLineToPoint:end]; 
    return path; 
} 

结果:

enter image description here