2012-07-12 84 views
2

我要平滑的手指觸摸畫線曲線,我想在UIBezierPath解決方案僅我的代碼不平滑這裏行completely.my代碼是UIBezierPath平滑曲線手指觸摸繪圖

@implementation MyLineDrawingView 
@synthesize undoSteps; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     [super setBackgroundColor:[UIColor whiteColor]]; 

     pathArray=[[NSMutableArray alloc]init]; 
     bufferArray=[[NSMutableArray alloc]init]; 



    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 

    [[UIColor blackColor] setStroke]; 
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];  
} 
#pragma mark - Touch Methods 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [bufferArray removeAllObjects]; 
    myPath=[[UIBezierPath alloc]init]; 
    myPath.lineWidth=5; 
    myPath.miterLimit=-10; 
    myPath.lineCapStyle = kCGLineCapRound; 
    myPath.flatness = 0.0; 


    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]; 

} 

-(void)undoButtonClicked 
{ 
    if([pathArray count]>0) 
    { 
    UIBezierPath *_path=[pathArray lastObject]; 
    [bufferArray addObject:_path]; 
    [pathArray removeLastObject]; 
    [self setNeedsDisplay]; 
    } 

} 
-(void)redoButtonClicked 
{ 
    if([bufferArray count]>0){ 
     UIBezierPath *_path=[bufferArray lastObject]; 
     [pathArray addObject:_path]; 
     [bufferArray removeLastObject]; 
     [self setNeedsDisplay]; 
    } 
} 
- (void)dealloc 
{ 
    [pathArray release]; 
    [bufferArray release]; 
    [super dealloc]; 
} 

@end 

,我使用獲得輸出我的代碼像下面的屏幕截圖:

enter image description here

我要像下面的屏幕截圖平滑曲線輸出:

enter image description here

任何人都可以幫助我非常感謝!

在此先感謝!

回答

4

問題是,您只是使用addLineToPoint:向路徑添加點。這相當於創建一系列直線。您確實需要添加控制點,例如在Illustrator或Sketch中繪製曲線路徑時可能拖動的手柄。

您可以使用addCurveToPoint:controlPoint1:controlPoint2:添加這些;技巧是找出控制點的位置,相對於您從touches*事件中實際獲得的點數。

對於可能的技術的一個很好的討論,我推薦以下問題:Drawing Smooth Curves - Methods Needed