2011-01-24 13 views
8

Me gustaría desarrollar una aplicación cuando el usuario puede dibujar líneas ... pero no quiero dibujar líneas rectas, pero quiero mostrar la línea a medida que los usuarios la dibujan. Cuando el usuario pasa del punto A al B, me gustaría enderezar la línea (si los usuarios quieren esto).Dibujando una línea en iPhone/iPad

Para poder hacer esto, quiero cambiar mi vista a una grilla comenzando en 0,0 (arriba a la izquierda) y terminando en 320,480 (para iPhone) y 768,1024 (para iPad) (abajo a la derecha).

Para esta pregunta tengo el punto A en 10,10 y el punto B en 100,100.

Mi pregunta es:
- ¿Cómo creo esta grilla?
- ¿Cómo creo estos puntos?
- ¿Cómo dibujo esta línea sin enderezarla?
- ¿Cómo se dibuja la línea recta?

Mi problema es que estoy familiarizado con la creación de aplicaciones de IU "normales". No estoy familiarizado con Open-GL ect.

Espero que alguien me pueda ayudar con esto.

Saludos,
Paul Peelen

+0

Recomiendo resolver su problema de "enderezamiento" en otra pregunta. – genpfault

+0

Ok, ¿quieres decir que empiezo con líneas rectas como paso 1, y luego continúo con líneas no rectas en el paso 2? –

+0

Ha etiquetado esto como 'opengl-es' - ¿está haciendo su dibujo con eso, o con el sistema de dibujo" nativo ", Quartz? –

Respuesta

17

subclase su UIView y reemplazar el método - (void)drawRect:(CGRect)rect.

En no agarras un contexto gráfico:

CGContextRef context = UIGraphicsGetCurrentContext(); 

y utiliza eso para hacer núcleo gráfico llama, como:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextBeginPath (context); 
for (k = 0; k < count; k += 2) { 
    CGContextMoveToPoint(context, s[k].x, s[k].y); 
    CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y); 
} 
CGContextStrokePath(context); 

Busque la Guía de programación Quartz 2D para todos los detalles.

+0

Impresionante, echaré un vistazo. –

+0

Estoy empezando con lo mismo ,, cualquier ejemplo o tutorial ... no puedo entender cómo usar este código ... Cualquier ayuda apreciada gracias – Dave

+0

¿Alguien me ayuda a usar el código anterior? –

0

Puede arrastrar línea recta cuando el usuario arrastre que sobre la base de partida y el punto final dibujar una línea usando UIBezierPath y CAShapeLayer:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    startingPoint = [touch locationInView:baseHolderView]; 

} 
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    endingPoint = [touch locationInView:baseHolderView]; 
    [self makeLineLayer:baseHolderView.layer lineFromPointA:startingPoint toPointB:endingPoint]; 
} 

-(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB 
{ 
    CAShapeLayer *line = [CAShapeLayer layer]; 
    UIBezierPath *linePath=[UIBezierPath bezierPath]; 
    [linePath moveToPoint: pointA]; 
    [linePath addLineToPoint:pointB]; 
    line.path=linePath.CGPath; 
    line.fillColor = nil; 
    line.opacity = 2.0; 
    line.strokeColor = [UIColor blackColor].CGColor; 
    [layer addSublayer:line]; 
} 

la esperanza que esto ayudará a lograr su objetivo.

Cuestiones relacionadas