Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog
Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog
Tutorial pratici per iPhone SDK v1.6 - Get a Free Blog
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
12<br />
13<br />
14<br />
15<br />
16<br />
17<br />
18<br />
19<br />
20<br />
21<br />
22<br />
23<br />
24<br />
25<br />
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {<br />
! UITouch *touch = [touches anyObject];!<br />
! CGPoint puntoCorrente = [touch locationInView:viewDisegno];<br />
!<br />
! // definiamo il contest grafico<br />
! UIGraphicsBeginImageContext(viewDisegno.frame.size);<br />
! [viewDisegno.image drawInRect:CGRectMake(0, 0,<br />
viewDisegno.frame.size.width, viewDisegno.frame.size.height)];<br />
! // settiamo la forma e la dimensione del pennello<br />
! CGContextSetLineCap(UIGraphics<strong>Get</strong>CurrentContext(), kCGLineCapRound);<br />
! CGContextSetLineWidth(UIGraphics<strong>Get</strong>CurrentContext(), dimensione-<br />
Pennello);<br />
! // convertiamo il colore e impostiamolo come colore del pennello<br />
! const CGFloat *components = CGColor<strong>Get</strong>Components([colorePennello<br />
CGColor]);<br />
! CGContextSetRGBStrokeColor(UIGraphics<strong>Get</strong>CurrentContext(), components[0],<br />
components[1], components[2], components[3]);<br />
! // disegna il <strong>per</strong>corso<br />
! CGContextBeginPath(UIGraphics<strong>Get</strong>CurrentContext());<br />
! CGContextMoveToPoint(UIGraphics<strong>Get</strong>CurrentContext(),<br />
ultimoPunto.x, ultimoPunto.y);<br />
! CGContextAddLineToPoint(UIGraphics<strong>Get</strong>CurrentContext(),<br />
puntoCorrente.x, puntoCorrente.y);<br />
! CGContextStrokePath(UIGraphics<strong>Get</strong>CurrentContext());<br />
! // settiamo il disegno appena creato<br />
! viewDisegno.image = UIGraphics<strong>Get</strong>ImageFromCurrentImageContext();<br />
! UIGraphicsEndImageContext();<br />
!<br />
! ultimoPunto = puntoCorrente;!<br />
}<br />
Iniziamo ad analizzare passo passo il codice che abbiamo appena scritto.<br />
Alla riga 7 troviamo la definizione di un “Graphics Contexts”. Questo è un aspetto complesso<br />
del Core Graphics, ma utilizzando questa funzione lo adattiamo semplicemente alla nostra<br />
UIImageView, che infatti gli passiamo come parametro. Questo ci <strong>per</strong>metterà di disegnare all’interno<br />
della nostra immagine. Con l’istruzione seguente, infatti, definiamo l’area in cui potremo<br />
disegnare: essa avrà la stessa dimensione dell’immagine “viewDisegno”.<br />
Le righe 10 e 11 si riferiscono alla definizione del “pennello”, con cui l’utente disegnerà sullo<br />
schermo. Prima, infatti, viene impostata la forma (in questo caso “kCGLineCapRound” è di<br />
forma circolare, ma possiamo anche farlo quadrato o triangolare, vi basta guardare la documentazione<br />
<strong>per</strong> vedere gli altri valori possibili), poi la dimensione, passando ovviamente la variabile<br />
“dimensionePennello”, che contiene proprio il valore desiderato (riga 11).<br />
Le righe 13 e 14 servono <strong>per</strong> impostare il colore al nostro pennello. Abbiamo visto prima che il<br />
colore è definito da un elemento UIColor. Purtroppo dobbiamo convertire le varie componenti<br />
in CGFloat, e l’unico modo che ho trovato è quello che vedete nel codice. Prima convertiamo<br />
<strong>Tutorial</strong> <strong>pratici</strong> <strong>per</strong> <strong>iPhone</strong> <strong>SDK</strong>! 168