Tengo problemas para implementar "Efectos de Relieve/Sombra" en mi dibujo. funcionalidad pintura de dedos está trabajando bien con mi costumbre UIView
y por debajo es mi código drawRect
método:Cómo crear efectos en relieve o en la sombra con Core Graphics (para pintura de dedos)
Edición de código con todos los métodos:
- (void)drawRect:(CGRect)rect
{
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSaveGState(context);
// for shadow effects
CGContextSetShadowWithColor(context, CGSizeMake(0, 2),3, self.lineColor.CGColor);
CGContextStrokePath(context);
[super drawRect:rect];
}
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
previousPoint2 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
[self touchesMoved:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
// calculate mid point
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(path);
CGPathRelease(path);
CGRect drawBox = bounds;
//Pad our values so the bounding box respects our line width
drawBox.origin.x -= self.lineWidth * 2;
drawBox.origin.y -= self.lineWidth * 2;
drawBox.size.width += self.lineWidth * 4;
drawBox.size.height += self.lineWidth * 4;
UIGraphicsBeginImageContext(drawBox.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
curImage = UIGraphicsGetImageFromCurrentImageContext();
[curImage retain];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:drawBox];
}
cuando he implementado este estoy consiguiendo efectos de pintura con puntos dot dot ...
Vea a continuación la imagen (que no tiene sombras ni efectos en relieve). Si tienes alguna idea de cómo agregar estos efectos, por favor dame alguna sugerencia. ¿Cómo puedo resolver esto?
apreciamos su sugerencia. He usado UIBezierPath y lo he implementado, pero nos enfrentamos a un problema como la funcionalidad de borrar y elegir diferentes colores de pintura ... así que finalmente he cambiado la funcionalidad y uso CGContext. ahora toda la funcionalidad, como Borrar, cambiar el color del pincel está funcionando bien. El problema es solo para crear Relieve o efectos de sombras. que los efectos no funcionan correctamente ... la línea recta funciona bien con efectos de sombra. Pero los efectos de curva (suave) no funcionan bien. Por favor dígame alguna sugerencia usando CGContext. – Hitarth
Pregunta rápida: ¿Cómo desea que funcione Erase? La mayoría de los programas de 'dibujo' borrarán cada 'ruta' separada (como se define como un toque de un dedo/evento de arrastrar/finalizar). Esto funciona con mi sugerencia/respuesta. Sin embargo, si intenta borrar cada punto de ruta o incluso cada píxel ... eso será mucho más difícil de lograr. –
estoy borrando usando el contexto actual. y funciona bien – Hitarth