次のコードを使用して、タッチイベントで画像に色を付けています。サブクラスの uiview があり、次のコードを使用してそれを行います。機能していますが、画像のさまざまな部分で色が異なります。画像を赤色で着色していて、緑色があるとします。画像の異なる部分に黄色または他の色が存在します。次に、赤色は画像に既に存在する色に基づいて異なる効果を持ちます。代わりに、同じ赤色(または選択された色)を画像全体に塗りつぶします。私はそれを達成しますか?
@synthesize selImage,isReset;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
arrBezierPaths = [[NSMutableArray alloc] init];
globalPath = [[UIBezierPath alloc] init];
myPath = [[UIBezierPath alloc] init];
self.userInteractionEnabled = TRUE;
myPath.lineWidth = 30;
brushPattern = [UIColor redColor];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:myPath, @"Path", brushPattern, @"Color", nil];
[arrBezierPaths addObject:dict];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
for (int i=0; i<[arrBezierPaths count]; i++)
{
NSDictionary *dict = [arrBezierPaths objectAtIndex:i];
UIColor *tempBrushpatter = [[UIColor alloc] init];
tempBrushpatter = [dict valueForKey:@"Color"];
globalPath = [dict valueForKey:@"Path"];
[globalPath strokeWithBlendMode:kCGBlendModeOverlay alpha:1.0];
[tempBrushpatter setStroke];
}
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
[globalPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
CGPoint pointTouched = [mytouch locationInView:self];
for (int i=0; i<[arrBezierPaths count]; i++)
{
UIBezierPath *tempErasePath = [[UIBezierPath alloc] init];
NSDictionary *dictTemp = [arrBezierPaths objectAtIndex:0];
UIBezierPath *temppath = [dictTemp valueForKey:@"Path"];
if ([temppath containsPoint:pointTouched])
{
//[temppath removeAllPoints];
}
}
[globalPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)changeColor:(UIColor *)color
{
[arrRemovePaths removeAllObjects];
UIBezierPath *temp = [[UIBezierPath alloc] init];
temp.lineWidth = 30;
UIColor *brushColor = color;
NSLog(@"initWithFrame method called");
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:temp, @"Path", brushColor, @"Color", nil];
[temp release];
[arrBezierPaths addObject:dict];
brushPattern = [color retain];
[self setNeedsDisplay];
}
- (void)dealloc
{
// [brushPattern release];
[super dealloc];
}