2012-05-04 99 views
1

我使用UIBezierPath和UIView的-touches...方法編寫了一個簡單的繪圖代碼。繪圖效果很好,但我做了一些不好的經驗。UIBezierPath繪製線條表示

  1. 當我畫得很慢時,效果很好。但是我畫得越快,線條就越尖銳。那麼,我怎樣才能讓他們「順利」變得不那麼尖銳(多點)呢?

  2. 當我使用高線寬的setLineWidth時,該線變得非常難看

這裏是一個圖像顯示什麼醜陋實際上意味着: This actually means "ugly"

爲什麼會得出脂肪線以這種方式?

編輯:這裏的一些代碼

- (void)drawInRect:(CGRect)rect 
{ 
    for(UIBezierPath *path in pathArray) { 
     [[UIColor blackColor] setStroke]; 
     [path setLineWidth:50.0]; 
     [path stroke]; 
    } 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    startPoint = [[touches anyObject] locationInView:self]; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path moveToPoint:startPoint]; 
    [path addLineToPoint:[[touches anyObject] locationInView:self]]; 
    if(isDrawing) { 
     [pathArray removeLastObject]; 
    } 
    else { 
     isDrawing = YES; 
    } 
    [pathArray addObject:path]; 
    [path close]; 
    [self setNeedsDisplay]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    isDrawing = NO; 
} 

我希望有人能幫助我處理這些問題。感謝很多人,祝賀Julian

+0

你能分享設置路徑的代碼嗎? – mprivat

+0

我添加了一些代碼。我是從記憶中寫下來的,所以在某些時候可能會拼寫錯誤 –

回答

1

嗯,我不打算解決你在這個線程中執行的性能問題,但是看看UIView中的setNeedsDisplayInRect:,一旦你獲得了基本的工作。

我想你基本上是想從你的陣列中取出最後一個創建的路徑,並用一個新的路徑代替它,只要你移動。

您應該嘗試將CGMutablePathRef放入您的陣列中(請參閱CGMutablePathRef to NSMutableArray)。

的基本流程是這樣的:

  1. touchesBegan

    1. 在陣列中創建CGMutablePathRef
    2. 呼叫moveToPoint
    3. 商店
  2. touchesMoved

    1. 從數組中獲取lastObject(即,在CGMutablePathRef
    2. 呼叫addLineToPoint
    3. [self setNeedsDisplay]

然後在drawInRect,遍歷的路徑和畫他們。

再說一遍,起初會很慢,那麼你需要優化。 CGLayerRef可以提供幫助。 setNeedsDisplayInRect最肯定也會。