20.04.2013 Views

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

SHOW MORE
SHOW LESS

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 />

#import <br />

@interface TrashAppViewController : UIViewController {<br />

! IBOutlet UIImageView *imageCestino;<br />

! IBOutlet UIImageView *imageLogo;<br />

!<br />

! BOOL cancellato;<br />

}<br />

-(IBAction)ripristina;<br />

-(void)cancella;<br />

Alla riga 7 abbiamo aggiunto una variabile che ci servirà <strong>per</strong> controllare se il nostro oggetto è<br />

già stato eliminato (quindi, al valore YES corrisponderà il logo eliminato, con NO il nostro logo<br />

sarà ancora visibile); lo stesso vale <strong>per</strong> il metodo “cancella”, che si occu<strong>per</strong>à invece dell’animazione<br />

legata all’eliminazione del logo.<br />

Ora iniziamo ad implementare i metodi necessari. Apriamo il file “TrashAppViewController.m”<br />

e inseriamo il seguente codice:<br />

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 />

26<br />

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {<br />

! UITouch *touch = [[event allTouches] anyObject];<br />

! if ([touch view] == imageLogo) {<br />

! ! imageLogo.center = [touch locationInView:self.view];!<br />

! }! !<br />

}<br />

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {<br />

! if (CGRectIntersectsRect([imageCestino frame], [imageLogo<br />

frame])){<br />

! ! imageCestino.image = [UIImage<br />

imageNamed:@"TrashIconFull.png"];<br />

! ! [self cancella];<br />

! } else{<br />

! ! [UIView beginAnimations:nil context:NULL];<br />

! ! [UIView setAnimationDuration:0.5];<br />

! ! imageLogo.center=CGPointMake(155.0,100.0);<br />

! ! [UIView commitAnimations];<br />

! }<br />

}<br />

- (void)cancella{<br />

! cancellato = YES;<br />

! [UIView beginAnimations:nil context:NULL];<br />

! [UIView setAnimationDuration:0.5];<br />

! imageLogo.transform = CGAffineTransformMakeScale(.001, .001);<br />

! [UIView commitAnimations];<br />

}<br />

<strong>Tutorial</strong> <strong>pratici</strong> <strong>per</strong> <strong>iPhone</strong> <strong>SDK</strong>! 32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!