3

指で触れた描画線の曲線を滑らかにしたいのですが、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

以下のスクリーンショットのように、コードを使用して出力を取得しています。

ここに画像の説明を入力してください

以下のスクリーンショットのような滑らかな曲線出力が必要です。

ここに画像の説明を入力してください

誰かが私を大いに感謝するのを手伝ってくれる?

前もって感謝します!

4

1 に答える 1

5

問題は、を使用してパスにポイントを追加するだけであるということですaddLineToPoint:。これは、一連の直線を作成するのと同じです。IllustratorやSketchで曲線のパスを描画するときにドラッグする可能性のあるハンドルなど、コントロールポイントを追加する必要があります。

addCurveToPoint:controlPoint1:controlPoint2:それらを追加するために使用できます。touches*秘訣は、イベントから実際に取得するポイントと比較して、コントロールポイントを配置する場所を見つけることです。

可能なテクニックの良い議論のために、私は次の質問をお勧めします:滑らかな曲線を描く-必要な方法

于 2012-07-12T12:23:37.543 に答える