3

これが私が現在使用している関連する.mです。

- (void)drawRect:(CGRect)rect
{

    [[UIColor redColor] setStroke];
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];    


}

#pragma mark -
#pragma mark - Touch Methods


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    myPath=[[UIBezierPath alloc]init];


    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }

    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];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    if([ud objectForKey:@"lineThickness"] == nil) {
        myPath.lineWidth=5;
    }
    else {

        float thicknessFloat = [ud floatForKey:@"lineThickness"];
        myPath.lineWidth= 10. * thicknessFloat;


    }
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];

}

うまく機能しますが、これは私が少し変更したチュートリアルコードであるため、2つのポイント間に線を引きたいという問題に対処する方法がわかりません。また、ポイントが追加されるたびにフレームワークでポイントを接続します。

誰かがこれを達成する方法について私に良い方向を教えてもらえますか?

4

2 に答える 2

3

これを実装する方法の詳細は、探している効果によって異なります。たくさんのポイントをタップしてそれらをに追加したい場合は、ViewControllerでUIBezierPath次のようなことを行うことができます。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    // I'm assuming you have a myPath UIBezierPath which is an ivar which is 
    // initially nil. In that case, we'll check if it's nil and if so, initialize 
    // it, otherwise, it's already been initialized, then we know we're just
    // adding a line segment.

    if (!myPath)
    {
        myPath = [UIBezierPath bezierPath];
        [myPath moveToPoint:location];

        shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
        shapeLayer.lineWidth = 1.0;
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;

        [self.view.layer addSublayer:shapeLayer];
    }
    else
    {
        [myPath addLineToPoint:location];
        shapeLayer.path = myPath.CGPath;
    }
}

指で描くことができるものが必要な場合(たとえば、指で描くドラッグ)、次のようになります。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    myPath = [UIBezierPath bezierPath];
    [myPath moveToPoint:location];

    shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.view.layer];
    shapeLayer.lineWidth = 1.0;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;

    [self.view.layer addSublayer:shapeLayer];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [mytouch locationInView:self.view];

    [myPath addLineToPoint:location];
    shapeLayer.path = myPath.CGPath;
}
于 2012-10-19T23:00:53.667 に答える
-1

UIBezierPathは曲線のパスを描画することを目的としているため、使用しません。

これを実現する最も効率的な方法は、描画するポイントを格納するために配列を使用しながら、drawRect内でコアグラフィックス描画コマンドを使用することです。この配列は、touchメソッドに追加されます。

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

    CGFloat black[4] = {0, 0, 
                    0, 1};
    CGContextSetStrokeColor(c, black);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 100, 100);
    CGContextAddLineToPoint(c, 100, 200); //call this in a loop that goes through the point array
    CGContextStrokePath(c);
}

ここにはもっと多くの情報があります:Quartz2Dプログラミングガイド

お役に立てれば!

于 2012-10-19T22:27:59.067 に答える