2011-06-22 43 views
5

这就是我想做的事:如何绘制UIBezierPaths

我有一个UIBezierPath,我想将它传递给一些方法为它绘制。或者只是从创建它的方法中绘制它。

我不知道如何来指示查看它应该被吸入。做所有的绘图方法已经开始与

- (void)drawRect:(CGRect)rect { ...} ? 

我可以做

- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ?? 

如何调用这个函数或方法,从另一种方法?

+0

参见[如何绘制自定义视图贝塞尔路径(http://stackoverflow.com/a/34659468/3681880之前你)以一个例子进行完整的解释。 – Suragch

回答

20

drawRect:是当您在视图上发送消息setNeedsDisplaysetNeedsDisplayInRect:时自动调用的东西。您从不直接拨打drawRect:

但是你说得对,所有的绘图操作都是在drawRect:方法中完成的。典型的实现将是

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

    /* Do your drawing on `context` */ 
} 

由于您使用UIBezierPath S,你需要保持的,你将需要绘制,然后调用setNeedsDisplay当有些变化贝塞尔路径的数组。

- (void)drawRect:(CGRect)rect {  
    for (UIBezierPath * path in bezierPaths) { 
     /* set stroke color and fill color for the path */ 
     [path fill]; 
     [path stroke]; 
    } 
} 

其中bezierPathsUIBezierPath s的数组。

+0

我明白了。在哪部分代码中,我需要创建UIBezierPath数组? –

+0

将它声明为您视图中的属性。 –

+0

谢谢,这非常有帮助,我完成了这项工作(现在有点儿)。 –

3

当你需要定制您的看法,您可以在子类覆盖-drawRect:

- (void)drawRect:(CGRect)rect 
{ 
    // config your context 
    [bezierPath stroke]; 
} 

编辑:直接使用-stroke使代码更紧凑。

+1

好奇 - 你使用CoreGraphics方法而不是仅仅使用' - [UIBezierPath stroke]'的任何原因? –

+0

@Ben:谢谢,我很想念它。 – cxa

+1

如果您打算使用UIKit完成所有操作,则甚至不需要获取上下文。 –

6

首先,保存路径在实例变量

@interface SomeView { 
    UIBezierPath * bezierPath; 
} 
@property(nonatomic,retain) UIBezierPath * bezierPath; 
... 
@end 
.... 
- (void)someMethod { 
    self.bezierPath = yourBezierPath; 
    [self setNeedsDisplayInRect:rectToRedraw]; 
} 
在-drawRect

- (void)drawRect:(CGRect)rect { 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(currentContext, 3.0); 
    CGContextSetLineCap(currentContext, kCGLineCapRound); 
    CGContextSetLineJoin(currentContext, kCGLineJoinRound); 
    CGContextBeginPath(currentContext); 
    CGContextAddPath(currentContext, bezierPath.CGPath); 
    CGContextDrawPath(currentContext, kCGPathStroke); 
} 
1

绘图仅称为-drawRect:方法内发生(当一个视图被标记为需要被自动调用通过setNeedsDisplay显示)。所以一个drawRect:withBezierPath:方法将永远不会被自动调用。它会执行的唯一方法是如果你自己调用它。

一旦你有一个UIBezierPath,但是,它很容易得出它:

- (void)drawRect:(CGRect)rect { 
    UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar? 
    [path stroke]; 
} 

没有必要核芯显卡,以futz身边,如果你想要做的就是绘制路径。

+0

似乎是NSBezierPath不适用于iOS?我尝试了,但有错误。 –

1

你可以做下面的事情。只需定义一个UIColor * setStroke;在.h文件中,你需要设置则strokeColor对象调用[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

- (void)drawRect:(CGRect)rect 
    { 
     [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor) 
     for(UIBezierPath *_path in pathArray) 
      [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
    } 

    #pragma mark - Touch Methods 
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     myPath=[[UIBezierPath alloc]init]; 
     myPath.lineWidth = currentSliderValue; 

     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath moveToPoint:[mytouch locationInView:self]]; 
     [pathArray addObject:myPath]; 
    } 
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath addLineToPoint:[mytouch locationInView:self]]; 
     [self setNeedsDisplay]; 

    }