2

iPad アプリでのフリーハンド描画に UIBezierPath を使用しています。に消しゴムを適用したい。

ただし、パス内の図面のみを消去したいです。背景に他の要素があるため、パスの色を背景色として使用できません。

以下は、フリーハンドの図面を作成する方法です。

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        lineWidths = 10;
        brushPattern = [UIColor greenColor]; 
        pathArray = [[NSMutableArray alloc]init];
        bufferArray = [[NSMutableArray alloc]init];
        self.multipleTouchEnabled = NO;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {
    for (NSMutableDictionary *dictionary in pathArray) {
        UIBezierPath *_path = [dictionary objectForKey:@"Path"];
        UIColor *_colors = [dictionary objectForKey:@"Colors"];
        [_colors setStroke];
        _path.lineCapStyle = kCGLineCapRound;
        [_path stroke];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    myPath = [[UIBezierPath alloc]init];
    myPath.lineWidth = lineWidths;
    CGPoint touchPoint = [[touches anyObject] locationInView:self];

    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [myPath addLineToPoint:CGPointMake(touchPoint.x, touchPoint.y)];

    dict = @{@"Path": myPath, @"Colors": brushPattern};
    [pathArray addObject:dict];

    [self setNeedsDisplay];

    [undoManager registerUndoWithTarget:self selector:@selector(undoButtonClicked) object:nil];
}

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

2 に答える 2

8

消去用の BOOL 値を保存します。BOOL _erase;

BOOL eraseButtonIsTapped = ...
if eraseButtonIsTapped {
    _erase = yes;
} else{
    _erase = NO;
}

描画時:

[myPath strokeWithBlendMode:_erase?kCGBlendModeClear:kCGBlendModeNormal alpha:1.0f];
于 2014-05-20T07:58:59.537 に答える
4

これを試してみてください

    brushPattern = view.backgroundColor;

これにより、描画されたパスの正確な背後にある色で新しい線が描画されます。そして、同じ pathArray を使用してこれを行うことができます。そのため、後でやり直し/元に戻す操作も実装できます。必要に応じて、これについて詳しく説明できます。

于 2012-08-01T10:54:17.223 に答える