私はIOSの描画アプリにいくつかの問題があります。私はいくつかのチュートリアルの助けを借りて自由な手描きを作成しました。しかし、私は図面を消去する際にいくつかの困難を発見しました。私のアプリでは、背景画像として消しゴムのボタンがあります。消しゴムボタンをクリックすると、図面上をスワイプすると、スワイプするたびに図面が消去されます。誰でも私にこれを手伝うことができますか? ありがとうございます。下記のボタンをクリックして図面を消去するには?
は私のコードです:
@implementation LinearInterpView
{
UIBezierPath *path;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self setMultipleTouchEnabled:YES];
[self setBackgroundColor:[UIColor whiteColor]];
path=[UIBezierPath bezierPath];
[path setLineWidth:2.0];
}
return self;
}
-(void)drawRect:(CGRect)rect{
[[UIColor blackColor] setStroke];
[path stroke];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
CGPoint p=[touch locationInView:self];
[path moveToPoint:p];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[touches anyObject];
CGPoint p=[touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesEnded:touches withEvent:event];
}
// This is the button action to erase the drawing.
- (IBAction)erase:(id)sender {
CGContextRef cgref=UIGraphicsGetCurrentContext();
CGContextSetBlendMode(cgref, kCGBlendModeClear);
}
は親切に私が何をしたかのミス、私をオフにします。
のdrawRectは毎回呼び出されるあなたはストロークを作っているとき、あなたのdrawRectであなたはそれが呼び出されている黒毎回のようにストロークの色を設定しているので、したがって、私がしたいです消去ボタンを押して消去を試みると、黒いストロークを取得するかどうかを尋ねます。 – azmuhak
はい、絶対にストロークは黒色です。私は疑問も持っています。私が消したい場合は、drawRectメソッドを呼び出す必要があるということです。それは、黒の色で図面に影響を与えずに、コード内で行われる変更は何ですか?また、線を消去します。 –
うーん、簡単な方法は、bool値を使用し、消去ボタンアクションではboolを設定し、draw rectメソッドではブール値に応じてストロークの色を黒または白に設定します。 – azmuhak